Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript Return Statements

JavaScript Return Statements

JavaScript Statement Reference JavaScript statement reference manual

Example

Returns the value of PI:

function

myFunction

(

)

{

return

Math

.

PI

;

}

Output result:

3.141592653589793

Try it »

The bottom of this article contains more examples.


Definition and Usage

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

Please read our JavaScript tutorial to learn more about functions. First, we can get to know JavaScript Functions and JavaScript Scope. For more detailed content, see Function definition, Parameters, called and Closures.


Browser support

Statement
return Yes Yes Yes Yes Yes

Syntax

return [[expression]]; 

Returns the value of expression. If omitted, i.e. return;, it returns undefined.

The following return statements all terminate function execution:

return;

return true;

return false;

return x;

return x + y / 3;

Parameter value

Parameters Description
value Optional. Specifies the value to return from the function. If omitted, returns undefined

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 and assign the return value to variable x

function myFunction(a, b) {

    return a * b; // The function returns the product of a and b

}

x

The output is:

12

Try it »

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 Manual: JavaScript function Statement


JavaScript Statement Reference JavaScript statement reference manual