Description
When a return
statement is used in a function body, the execution of the
function is stopped. If specified, a given value is returned to the function caller. For
example, the following function returns the square of its argument, x
,
where x
is a number.
function square(x) { return x * x; } const demo = square(3); // demo will equal 9
If the value is omitted, undefined
is returned instead.
The following return statements all break the function execution:
return; return true; return false; return x; return x + y / 3;
Automatic Semicolon Insertion
The return
statement is affected by
automatic semicolon insertion (ASI).
No line terminator is allowed between the return
keyword and the expression.
return a + b;
is transformed by ASI into:
return; a + b;
The console will warn "unreachable code after return statement".
Note: Starting with Firefox 40, a warning is shown in the console if
unreachable code is found after a return
statement.
To avoid this problem (to prevent ASI), you could use parentheses:
return ( a + b );