JavaScript returnStatement

JavaScript 语句参考手册JavaScript Statement Reference

Examples

Return the value of PI:

function myFunction() { return Math.PI; }

Output:

3.141592653589793

Try it Yourself »

This article contains more examples at the bottom.


Definition and Usage

The return statement stops the execution of a function and returns the value of the function.

Please read our JavaScript tutorial to learn more about functions. First, we can start by understandingJavaScript FunctionsandJavaScript Scope. For more detailed content, you can viewFunction Definitions, Parameters, InvocationandClosures。


Browser Support

Statement
return Yes Yes Yes Yes Yes


Syntax

return [[expression]]; 

Return the value of expression. If omitted, i.e.return;then undefined is returned.

The following return statements all terminate the execution of the function:

return;
return true;
return false;
return x;
return x + y / 3;

Parameter Values

Parameter Description
value Optional. Specifies the returned function value. If omitted, will returnundefined

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, assign the returned value to variable x

function myFunction(a, b) {
return a * b; // The function returns the product of a and b
}
xThe output is:
12

Try it Yourself »

Return 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 Definitions

JavaScript Tutorial:JavaScript Function Parameters

JavaScript Tutorial:JavaScript Function Invocation

JavaScript Tutorial:JavaScript Function Closures

JavaScript Reference:JavaScript function Statement


JavaScript 语句参考手册JavaScript Statements Reference Other Extensions