JavaScript return
Examples
Return the value of PI:
return Math.PI;
}
Return "Hello John":
function myFunction(name) {
return "Hello " + name;
}
More examples below.
Description
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
Parameters
The value to be returned.
If omitted, it returns
undefined
More Examples
Calculate the product of two numbers and return the result:
var x = myFunction(4, 3);
function myFunction(a, b) {
// Return the product of a and b
return a * b;
}
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 (JavaScript 1997) feature.
It is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |