return
Ends function execution and optionally specifies a value to return.
Syntax
return;
return expression;
Returns
any — The value of `expression`, or `undefined` if no expression is given.
Examples
function double(n) {
return n * 2;
}
console.log(double(8));
Output
16
function early(x) {
if (x < 0) return "negative";
return "non-negative";
}
console.log(early(-1));
Output
negative
function noValue() {
return;
}
console.log(noValue());
Output
undefined
Notes
- Automatic semicolon insertion can break `return` if the expression
starts on the next line; keep the value on the same line as `return`.
- `return` is only valid inside a function body.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 1.0 |
| firefox | 1.0 |
| safari | 1.0 |
| edge | 12 |
| node | 0.10 |