CKTIS / JavaScript
return — exit a function and return a value
The return statement ends function execution and can send a result back to the caller. Learn about an empty return, early exits, and the newline pitfall.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
return;
return expression;return is a reserved keyword. In an ordinary function, return expression; evaluates the expression and returns its value to the caller. return; returns undefined; reaching the end of an ordinary function without return produces the same result. The statement is not allowed at the top level of a standard script or module.
Use an early return to exit a function after checking a condition. It exits the entire current function, not just an enclosing loop. In the example, doublePositive(0) exits before multiplication, whereas doublePositive(3) returns 6.
Do not put a line break between return and its expression. Automatic semicolon insertion can then end the statement as return;. For a multiline expression, start with something such as return ( and continue on subsequent lines.
A finally block runs before a pending return completes. Its own return or throw can replace that outcome, so avoid such overrides. return is ECMAScript syntax, not a Web API; the example requires no browser API.
Examples
function doublePositive(value) {
if (value <= 0) return;
return value * 2;
}
console.log(doublePositive(3));
console.log(doublePositive(0));Result
6 undefined
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.