Return values and early exit
Quick answer
The return statement ends the current function call and optionally supplies a value to its caller. A function that finishes without returning a value produces undefined.
How it works
Use return expression; for a result or return; for an early exit. Statements after an unconditional return in the same path are not executed. A line break immediately after return triggers automatic semicolon insertion.
function label(n) { if (n < 0) return "invalid"; return "ready"; }console.log(label(-2), label(3));Expected output in Node.js:
invalid readyThe guard returns before the normal path.
Expected output in Node.js:
undefinedThe line break makes return a statement without an expression.
Common mistakes
Logging is not returning. Keep the returned expression on the same line or start its parentheses there.
Practice
Create a function that returns zero for negative inputs and the number otherwise.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Return values and early exit — Knowledge Check
Apply the taught concept and reason about the result.
What does a function that reaches its end without returning a value produce?
What happens to statements in the same execution path after an unconditional return?
Why can a line break immediately after return change the result?
Does console.log(5) inside a function automatically make its return value 5?
What does function f(n){if(n<0)return 0;return n;} return for -3?