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 781815 of break

  • Revision slug: Web/JavaScript/Reference/Statements/break
  • Revision title: break
  • Revision id: 781815
  • Created:
  • Creator: tearwyx
  • Is current revision? No
  • Comment italics in syntax; indentation fix

Revision Content

{{jsSidebar("Statements")}}

Summary

The break statement terminates the current loop, {{jsxref("Statements/switch", "switch")}}, or {{jsxref("Statements/label", "label")}} statement and transfers program control to the statement following the terminated statement.

Syntax

break [label];
label
Optional. Identifier associated with the label of the statement.  If the statement is not a loop or {{jsxref("Statements/switch", "switch")}}, this is required.

Description

The break statement includes an optional label that allows the program to break out of a labeled statement. The break statement needs to be nested within the referenced label. The labeled statement can be any {{jsxref("Statements/block", "block")}} statement; it does not have to be preceded by a loop statement.

Examples

The following function has a break statement that terminates the {{jsxref("Statements/while", "while")}} loop when i is 3, and then returns the value 3 * x.

function testBreak(x) {
  var i = 0;

  while (i < 6) {
    if (i == 3) {
      break;
    }
    i += 1;
  }

  return i * x;
}

The following code uses break statements with labeled blocks. A break statement must be nested within any label it references. Notice that inner_block is nested within outer_block.

outer_block: {
  inner_block: {
    console.log('1');
    break outer_block; // breaks out of both inner_block and outer_block
    console.log(':-('); // skipped
  }
  console.log('2'); // skipped
}

The following code also uses break statements with labeled blocks but generates a Syntax Error because its break statement is within block_1 but references block_2. A break statement must always be nested within any label it references.

block_1: {
  console.log('1');
  break block_2; // SyntaxError: label not found
}

block_2: {
  console.log('2');
}

Specifications

Specification Status Comment
ECMAScript 1st Edition Standard Initial definition. Unlabeled version.
ECMAScript 3rd Edition Standard Labeled version added.
{{SpecName('ES5.1', '#sec-12.8', 'Break statement')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-break-statement', 'Break 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

  • {{jsxref("Statements/continue", "continue")}}
  • {{jsxref("Statements/label", "label")}}
  • {{jsxref("Statements/switch", "switch")}}

Revision Source

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

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

<p>The <strong>break statement</strong> terminates the current loop, {{jsxref("Statements/switch", "switch")}}, or {{jsxref("Statements/label", "label")}} statement and transfers program control to the statement following the terminated statement.</p>

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

<pre class="syntaxbox">
<code>break [<em>label</em>];</code></pre>

<dl>
 <dt><code>label</code></dt>
 <dd>Optional. Identifier associated with the label of the statement.&nbsp; If the statement is not a loop or {{jsxref("Statements/switch", "switch")}}, this is required.</dd>
</dl>

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

<p>The <code>break</code> statement includes an optional label that allows the program to break out of a labeled statement. The <code>break</code> statement needs to be nested within the referenced label. The labeled statement can be any {{jsxref("Statements/block", "block")}} statement; it does not have to be preceded by a loop statement.</p>

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

<p>The following function has a <code>break</code> statement that terminates the {{jsxref("Statements/while", "while")}} loop when <code>i</code> is 3, and then returns the value 3 * <code>x</code>.</p>

<pre class="brush:js;highlight:[6];">
function testBreak(x) {
  var i = 0;

  while (i &lt; 6) {
    if (i == 3) {
      break;
    }
    i += 1;
  }

  return i * x;
}</pre>

<p>The following code uses <code>break</code> statements with labeled blocks. A <code>break</code> statement must be nested within any label it references. Notice that <code>inner_block</code> is nested within <code>outer_block</code>.</p>

<pre class="brush:js;highlight:[1,2,4];">
outer_block: {
  inner_block: {
    console.log('1');
    break outer_block; // breaks out of both inner_block and outer_block
    console.log(':-('); // skipped
  }
  console.log('2'); // skipped
}
</pre>

<p>The following code also uses <code>break</code> statements with labeled blocks but generates a Syntax Error because its <code>break</code> statement is within <code>block_1</code> but references <code>block_2</code>. A <code>break</code> statement must always be nested within any label it references.</p>

<pre class="brush:js;highlight:[1,3,6];">
block_1: {
  console.log('1');
  break block_2; // SyntaxError: label not found
}

block_2: {
  console.log('2');
}
</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. Unlabeled version.</td>
  </tr>
  <tr>
   <td>ECMAScript 3rd Edition</td>
   <td>Standard</td>
   <td>Labeled version added.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-12.8', 'Break statement')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-break-statement', 'Break 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>{{jsxref("Statements/continue", "continue")}}</li>
 <li>{{jsxref("Statements/label", "label")}}</li>
 <li>{{jsxref("Statements/switch", "switch")}}</li>
</ul>
Revert to this revision