Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 847171 of return

  • Revision slug: Web/JavaScript/Reference/Statements/return
  • Revision title: return
  • Revision id: 847171
  • Created:
  • Creator: Wingpad
  • Is current revision? No
  • Comment

Revision Content

{{jsSidebar("Statements")}}

The return statement ends function execution and specifies a value to be returned to the function caller.

Syntax

return [[expression]]; 
expression
The expression to return. If omitted, undefined is returned instead.

Description

When a return statement is called in a function, the execution of this function is stopped. If specified, a given value is returned to the function caller. If the expression is omitted, undefined is returned instead. The following return statements all break the function execution:

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

Automatic semicolon insertion

The return statement is affected by automatic semicolon insertion (ASI). No line terminator is allowed between the return keyword and the expression.

return
a + b;

// is transformed by ASI into

return; 
a + b;

// Console warns "unreachable code after return statement".
Starting with Gecko 40 {{geckoRelease(40)}}, a warning is shown in the console if unreachable code is found after a return statement.

Examples

return

The following function returns the square of its argument, x, where x is a number.

function square(x) {
   return x * x;
}

Interrupt a function

A function immediately stops at the point where return is called.

function counter() {
  for (var count = 1; ; count++) {  // infinite loop
    console.log(count + "A"); // until 5
      if (count === 5) {          
        return;
      }
      console.log(count + "B");  // until 4
    }
  console.log(count + "C");  // never appears
}

counter();

// Output:
// 1A
// 1B
// 2A
// 2B
// 3A
// 3B
// 4A
// 4B
// 5A

Returning a function

See also the article about Closures.

function magic(x) {
  return function calc(x) { return x * 42 };
}

var answer = magic();
answer(1337); // 56154

Specifications

Specification Status Comment
{{SpecName('ES1')}} {{Spec2('ES1')}} Initial definition.
{{SpecName('ES5.1', '#sec-12.9', 'Return statement')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-return-statement', 'Return statement')}} {{Spec2('ES6')}}  

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}

See also

Revision Source

<div>{{jsSidebar("Statements")}}</div>

<p>The <strong><code>return</code> statement</strong> ends function execution and specifies a value to be returned to the function caller.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
return [[expression]]; </pre>

<dl>
 <dt><code>expression</code></dt>
 <dd>The expression to return. If omitted, <code>undefined</code> is returned instead.</dd>
</dl>

<h2 id="Description">Description</h2>

<p>When a <code>return</code> statement is called in a function, the execution of this function is stopped. If specified, a given value is returned to the function caller. If the expression is omitted, <code>undefined</code> is returned instead. The following return statements all break the function execution:</p>

<pre class="brush: js">
return;
return true;
return false;
return x;
return x + y / 3;
</pre>

<h3 id="Automatic_semicolon_insertion">Automatic semicolon insertion</h3>

<p>The <code>return</code> statement is affected by <a href="/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Automatic_semicolon_insertion">automatic semicolon insertion (ASI)</a>. No line terminator is allowed between the <code>return</code> keyword and the expression.</p>

<pre class="brush: js">
return
a + b;

// is transformed by ASI into

return; 
a + b;

// Console warns "unreachable code after return statement".
</pre>

<div class="note">Starting with Gecko 40 {{geckoRelease(40)}}, a warning is shown in the console if unreachable code is found after a return statement.</div>

<h2 id="Examples">Examples</h2>

<h3 id="return">return</h3>

<p>The following function returns the square of its argument, <code>x</code>, where <code>x</code> is a number.</p>

<pre class="brush: js">
function square(x) {
   return x * x;
}
</pre>

<h3 id="Interrupt_a_function">Interrupt a function</h3>

<p>A function immediately stops at the point where <code>return</code> is called.</p>

<pre class="brush: js">
function counter() {
  for (var count = 1; ; count++) {  // infinite loop
    console.log(count + "A"); // until 5
      if (count === 5) {          
        return;
      }
      console.log(count + "B");  // until 4
    }
  console.log(count + "C");  // never appears
}

counter();

// Output:
// 1A
// 1B
// 2A
// 2B
// 3A
// 3B
// 4A
// 4B
// 5A
</pre>

<h3 id="Returning_a_function">Returning a function</h3>

<p>See also the article about <a href="/en-US/docs/Web/JavaScript/Closures">Closures</a>.</p>

<pre class="brush: js">
function magic(x) {
  return function calc(x) { return x * 42 };
}

var answer = magic();
answer(1337); // 56154
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES1')}}</td>
   <td>{{Spec2('ES1')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-12.9', 'Return statement')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-return-statement', 'Return statement')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope" title="En/Core_JavaScript_1.5_Reference/Functions">Functions</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Closures">Closures</a></li>
</ul>
Revert to this revision