JavaScript return
Examples
Return the value of PI:
function myFunction() {
return Math.PI;
}
Try it Yourself »
Return "Hello John":
document.getElementById("demo").innerHTML = myFunction("John");
function myFunction(name) {
return "Hello " + name;
}
Try it Yourself »
More examples below.
Definition and Usage
The return
statement stops the execution of a function and returns a value.
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.
Syntax
return value;
Parameters
Parameter | Description |
value | Optional. The value to be returned. If omitted, it returns undefined |
More Examples
Calculate the product of two numbers and return the result:
// Call a function and save the return value in x:
var x = myFunction(4, 3);
function myFunction(a, b) {
// Return the product of a and b
return a * b;
}
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
Browser Support
return
is an ECMAScript1 (ES1) feature.
ES1 (JavaScript 1997) is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |