JavaScript return Statement
JavaScript Statements Reference
Example
Return the value of PI:
return Math.PI;
}
The result will be:
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
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:
return "Hello " + name;
}
document.getElementById("demo").innerHTML = myFunction("John");
The result will be:
Example
Calculate the product of two numbers, and return the result:
function myFunction(a, b) {
return a * b; // Function returns the product of a and b
}
The result of x will be:
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