mozilla

Revision 109071 of break

  • Revision slug: JavaScript/Reference/Statements/break
  • Revision title: break
  • Revision id: 109071
  • Created:
  • Creator: Bitwiseplatypus
  • Is current revision? No
  • Comment /* Example: Using <code>break</code> */ fix bad coding practices in example

Revision Content

Summary

Terminates the current loop, switch, or label statement and transfers program control to the statement following the terminated statement.

Statement
Implemented in: JavaScript 1.0, NES 2.0
ECMA Version: ECMA-262 (for the unlabeled version)

ECMA-262, Edition 3 (for the labeled version)

Syntax

break {{ mediawiki.external('label') }};

Parameters

label 
Identifier associated with the label of the statement.

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 this labelled statement. The labelled statement can be any type of statement; it does not have to be a loop statement.

Examples

Example: Using break

The following function has a break statement that terminates the 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;
}

See also

continue, label, switch


{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Sentencias/break", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Instructions/break", "ja": "ja/Core_JavaScript_1.5_Reference/Statements/break", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Polecenia/break" } ) }}

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p>Terminates the current loop, switch, or label statement and transfers program control to the statement following the terminated statement.
</p>
<table class="fullwidth-table">
<tbody><tr>
<td class="header" colspan="2">Statement</td>
</tr>
<tr>
<td>Implemented in:</td>
<td>JavaScript 1.0, NES 2.0</td>
</tr>
<tr>
<td>ECMA Version:</td>
<td>ECMA-262 (for the unlabeled version)
<p>ECMA-262, Edition 3 (for the labeled version)
</p>
</td>
</tr>
</tbody></table>
<h3 name="Syntax"> Syntax </h3>
<p><code>
break {{ mediawiki.external('<i>label</i>') }};
</code>
</p>
<h3 name="Parameters"> Parameters </h3>
<dl><dt> <code>label</code> </dt><dd> Identifier associated with the label of the statement.
</dd></dl>
<h3 name="Description"> Description </h3>
<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 this labelled statement. The labelled statement can be any type of statement; it does not have to be a loop statement.
</p>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Using_break"> Example: Using <code>break</code> </h4>
<p>The following function has a <code>break</code> statement that terminates the <code><a href="en/Core_JavaScript_1.5_Reference/Statements/while">while</a></code> loop when <code>i</code> is 3, and then returns the value 3 * <code>x</code>.
</p>
<pre class="eval">function testBreak(x) {
   var i = 0;
   while (i &lt; 6) {
      if (i == 3) {
         break;
      }
      i += 1;
   }
   return i * x;
}
</pre>
<h3 name="See_also"> See also </h3>
<p><a href="en/Core_JavaScript_1.5_Reference/Statements/continue">continue</a>,
<a href="en/Core_JavaScript_1.5_Reference/Statements/label">label</a>,
<a href="en/Core_JavaScript_1.5_Reference/Statements/switch">switch</a>
</p><p><br>
</p>
<div class="noinclude">
</div>
{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Sentencias/break", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Instructions/break", "ja": "ja/Core_JavaScript_1.5_Reference/Statements/break", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Polecenia/break" } ) }}
Revert to this revision