References

Beginner-friendly references for web development, with live, editable examples.

The JavaScript return statement

Statement JavaScript All modern browsers Updated
Quick answer

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

Live 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 return to exit a function at the top when there's nothing to do — it flattens nesting.
  • Keep the returned value on the same line as return to avoid automatic-semicolon bugs.
  • Remember code after a reached return never runs.
  • A function with no return yields undefined — fine for side-effect functions.

Frequently asked questions

What does return do in JavaScript?
It ends the function immediately and sends a value back to the caller. Code after it doesn't run.
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?
A likely cause is a line break right after 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?
Not directly, but you can return an array or object: return [a, b] or return { a, b }, then unpack it with destructuring.