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 780339 of return

  • Revision slug: Web/JavaScript/Reference/Statements/return
  • Revision title: return
  • Revision id: 780339
  • Created:
  • Creator: arai
  • Is current revision? No
  • Comment Bug 1005110 - Warn about unreachable code after semicolon-less return statement

Revision Content

{{jsSidebar("Statements")}}

Summary

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). There is no line terminator between the return keyword and the expression allowed.

Starting with Gecko 40 {{geckoRelease(40)}}, warning is shown in console if expression-like statement is found after semicolon-less return statement ({{bug(1005110)}}).
return
a + b;

// is transformed by ASI into

return; 
a + b;

Examples

Example: Using return

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

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

Example: 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

Example: Returning a function

To learn more about closures, read the JavaScript guide.

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

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

Specifications

Specification Status Comment
ECMAScript 1st Edition Standard 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>
<div>{{jsSidebar("Statements")}}</div>
</div>

<h2 id="Summary">Summary</h2>

<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" name="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>. There is no line terminator between the <code>return</code> keyword and the expression allowed.</p>

<div class="note">
Starting with Gecko 40 {{geckoRelease(40)}}, warning is shown in console if expression-like statement is found after semicolon-less return statement ({{bug(1005110)}}).
</div>

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

// is transformed by ASI into

return; 
a + b;
</pre>

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

<h3 id="Example:_Using_return" name="Example:_Using_return">Example: Using <code>return</code></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="Example:_Interrupt_a_function" name="Example:_Interrupt_a_function">Example: 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="Example.3A_Returning_a_function">Example: Returning a function</h3>

<p>To learn more about closures, read the <a href="/en-US/docs/Web/JavaScript/Guide/Closures">JavaScript guide</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>ECMAScript 1st Edition</td>
   <td>Standard</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" name="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/Guide/Closures">Closures</a></li>
</ul>
Revert to this revision