JavaScript return Statement
JavaScript Statements Reference
Example
Return the value of PI:
function myFunction() {
return Math.PI;
}
The result will be:
3.141592653589793
Try it Yourself »
More "Try it Yourself" examples below.
Definition and Usage
The return statement stops the execution of a function and returns a value from that function.
Read our JavaScript Tutorial to learn all you need to know about functions. Start with the introduction chapter about JavaScript Functions and JavaScript Scope. For more detailed information, see our Function Section on Function Definitions, Parameters, Invocation and Closures.
Browser Support
Statement | |||||
---|---|---|---|---|---|
return | Yes | Yes | Yes | Yes | Yes |
Syntax
return value;
Parameter Values
Parameter | Description |
---|---|
value | Optional. Specifies the value to be returned to the function caller. If omitted, it returns undefined |
Technical Details
JavaScript Version: | 1.0 |
---|
More Examples
Example
Use the return statement to display the name "John" in a <p> element:
function myFunction(name) {
return "Hello " + name;
}
document.getElementById("demo").innerHTML = myFunction("John");
The result will be:
Hello John
Try it Yourself »
Example
Calculate the product of two numbers, and return the result:
var x = myFunction(4, 3);
// Function is called, return value will end up in x
function myFunction(a, b) {
return a * b;
// Function returns the product of a and b
}
The result of x will be:
12
Try it Yourself »
Related Pages
JavaScript Tutorial: JavaScript Functions
JavaScript Tutorial: JavaScript Scope
JavaScript Tutorial: JavaScript Function Definitions
JavaScript Tutorial: JavaScript Function Parameters
JavaScript Tutorial: JavaScript Function Invocation
JavaScript Tutorial: JavaScript Function Closures
JavaScript Reference: JavaScript function Statement
JavaScript Statements Reference