JavaScript return
Statement
JavaScript Statements Reference
Example
Return the value of PI:
function myFunction() {
return Math.PI;
}
Output result:
3.141592653589793
More examples are included at the bottom of this article.
Definition and Usage
The return
statement terminates the execution of a function and returns a value from that function.
Read our JavaScript tutorial to learn more about functions. Start by understanding JavaScript Functions and JavaScript Scope. For more detailed information, see Function Definition, Parameters, Invocation, and Closures.
Browser Support
Statement | Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
return | Yes | Yes | Yes | Yes | Yes |
Syntax
return [[expression]];
Returns the value of expression
. If omitted, i.e., return;
, it returns undefined
.
The following return
statements all terminate the function's execution:
return;
return true;
return false;
return x;
return x + y / 3;
Parameter Values
Parameter | Description |
---|---|
value | Optional. Specifies the value to be returned from the function. If omitted, it returns undefined . |
Technical Details
| JavaScript Version: | 1.0 |
More Examples
Example
Calculate the product of two numbers and return the result:
var x = myFunction(4, 3);
// Call the function and assign the return value to variable x
function myFunction(a, b) {
return a * b;
// Function returns the product of a and b
}
Output result:
12
Returning a Function
function magic(x) {
return function calc(x) { return x * 42};
}
var answer = magic();
answer(1337); // 56154
Related Pages
JavaScript Tutorial: JavaScript Functions
JavaScript Tutorial: JavaScript Scope
JavaScript Tutorial: JavaScript Function Definition
JavaScript Tutorial: JavaScript Function Parameters
JavaScript Tutorial: JavaScript Function Invocation
JavaScript Tutorial: JavaScript Function Closures
JavaScript Reference: JavaScript function Statement