The JavaScript return statement
The return statement ends a function immediately and sends a value back to the caller. function double(n) { return n * 2; }. Code after return never runs. A function with no return (or a bare return;) gives back undefined.
Overview
return does two things at once: it stops the function right there, and it hands a value back to whoever called it. return n * 2 ends the function and makes double(5) evaluate to 10. Any code below a return that runs is unreachable — a useful fact for early exits, and a common source of "why isn't this running?" confusion.
That early-exit pattern is everywhere: if (!user) return; bails out at the top of a function when there's nothing to do, avoiding deep nesting. A bare return; with no value still ends the function, returning undefined — which is also what you get from a function that never returns at all.
One sharp edge to know: because of automatic semicolon insertion, you must keep the returned value on the same line as return. Writing return then the value on the next line makes JavaScript insert a semicolon after return, so the function returns undefined and the value below is dead code. Keep return and its expression together, or wrap multi-line values in parentheses.
Syntax
function name(params) {
// ...
return value; // ends here, sends value back
}
if (!valid) return; // early exit, returns undefined
Example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
function classify(n) {
if (n === 0) return 'zero'; // early exits
if (n < 0) return 'negative';
return 'positive';
}
document.getElementById('out').textContent =
[classify(-3), classify(0), classify(7)].join(', '); // negative, zero, positive
</script>
Best practices
- Use early
returnto exit a function at the top when there's nothing to do — it flattens nesting. - Keep the returned value on the same line as
returnto avoid automatic-semicolon bugs. - Remember code after a reached
returnnever runs. - A function with no
returnyieldsundefined— fine for side-effect functions.
Frequently asked questions
What does return do in JavaScript?
What does a function return with no return statement?
undefined. The same is true for a bare return; with no value.Why is my function returning undefined?
return: automatic semicolon insertion ends the statement, so the value below is ignored. Keep the value on the same line as return.Can I return multiple values?
return [a, b] or return { a, b }, then unpack it with destructuring.