JavaScript Return Statement

Definition and Usage

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

Please read our JavaScript tutorial to learn all about functions you need. Start with the chapters on JavaScript functions and JavaScript scope. For more detailed information, please refer to our tutorials on function definition, parameters, invocation, and closure.

Example

Return the value of PI:

function myFunction() {
  return Math.PI;
}

Try It Yourself

More TIY examples are available at the bottom of the page.

Syntax

return value;

Parameter Value

Parameter Description
value Optional. Specify the value to be returned to the function caller. If omitted, undefined is returned.

Technical Details

JavaScript Version: ECMAScript 1

More Examples

Example

Use the return statement to display the name "Bill" in the <p> element:

function myFunction(name) {
  return "Hello " + name;
}
document.getElementById("demo").innerHTML = myFunction("Bill");

Try It Yourself

Example

Calculate the product of two numbers and return the result:

var x = myFunction(4, 3);        // The function is called, and the value is returned to x
function myFunction(a, b) {
  return a * b;                // The function returns the product of a and b
}

Try It Yourself

Browser Support

Statement Chrome IE Firefox Safari Opera
return Support Support Support Support Support

Related Pages

JavaScript Tutorial:JavaScript Function

JavaScript Tutorial:JavaScript Scope

JavaScript Tutorial:JavaScript Function Definition

JavaScript Tutorial:JavaScript Function Parameters

JavaScript Tutorial:JavaScript Function Call

JavaScript Tutorial:JavaScript Function Closure

JavaScript Reference Manual:JavaScript function Statement