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 945825 of label

  • Revision slug: Web/JavaScript/Reference/Statements/label
  • Revision title: label
  • Revision id: 945825
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment add ES draft

Revision Content

{{jsSidebar("Statements")}}

The labeled statement can be used with {{jsxref("Statements/break", "break")}} or {{jsxref("Statements/continue", "continue")}} statements. It is prefixing a statement with an identifier which you can refer to.

Syntax

label :
   statement
label
Any JavaScript identifier that is not a reserved word.
statement
Statements. break can be used with any labeled statement, and continue can be used with looping labeled statements.

Description

You can use a label to identify a loop, and then use the break or continue statements to indicate whether a program should interrupt the loop or continue its execution.

Note that JavaScript has NO goto statement, you can only use labels with break or continue.

Examples

Using a labeled continue with for loops

var i, j;

loop1:
for (i = 0; i < 3; i++) {      //The first for statement is labeled "loop1"
   loop2:
   for (j = 0; j < 3; j++) {   //The second for statement is labeled "loop2"
      if (i == 1 && j == 1) {
         continue loop1;
      }
      console.log("i = " + i + ", j = " + j);
   }
}

// Output is:
//   "i = 0, j = 0"
//   "i = 0, j = 1"
//   "i = 0, j = 2"
//   "i = 1, j = 0"
//   "i = 2, j = 0"
//   "i = 2, j = 1"
//   "i = 2, j = 2"
// Notice how it skips both "i = 1, j = 1" and "i = 1, j = 2"

Using a labeled continue statement

Given an array of items and an array of tests, this example counts the number of items that passes all the tests.

var itemsPassed = 0;
var i, j;

top:
for (i = 0; i < items.length; i++){
  for (j = 0; j < tests.length; j++) {
    if (!tests[j].pass(items[i])) {
      continue top;
    }
  }

  itemsPassed++;
}

Using a labeled break with for loops

var i, j;

loop1:
for (i = 0; i < 3; i++) {      //The first for statement is labeled "loop1"
   loop2:
   for (j = 0; j < 3; j++) {   //The second for statement is labeled "loop2"
      if (i == 1 && j == 1) {
         break loop1;
      }
      console.log("i = " + i + ", j = " + j);
   }
}

// Output is:
//   "i = 0, j = 0"
//   "i = 0, j = 1"
//   "i = 0, j = 2"
//   "i = 1, j = 0"
// Notice the difference with the previous continue example

Using a labeled break statement

Given an array of items and an array of tests, this example determines whether all items pass all tests.

var allPass = true;
var i, j;

top:
for (i = 0; items.length; i++)
  for (j = 0; j < tests.length; i++)
    if (!tests[j].pass(items[i])){
      allPass = false;
      break top;
    }

Specifications

Specification Status Comment
{{SpecName('ES3')}} {{Spec2('ES3')}} Initial definition. Implemented in JavaScript 1.2
{{SpecName('ES5.1', '#sec-12.12', 'Labelled statement')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-labelled-statements', 'Labelled statement')}} {{Spec2('ES6')}}  
{{SpecName('ESDraft', '#sec-labelled-statements', 'Labelled statement')}} {{Spec2('ESDraft')}}  

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/break", "break")}}
  • {{jsxref("Statements/continue", "continue")}}

Revision Source

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

<p>The <strong>labeled statement</strong> can be used with {{jsxref("Statements/break", "break")}} or {{jsxref("Statements/continue", "continue")}} statements. It is prefixing a statement with an identifier which you can refer to.</p>

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

<pre class="syntaxbox">
<em>label</em> :
   <em>statement</em>
</pre>

<dl>
 <dt><code>label</code></dt>
 <dd>Any JavaScript identifier that is not a reserved word.</dd>
 <dt><code>statement</code></dt>
 <dd>Statements. <code>break</code> can be used with any labeled statement, and <code>continue</code> can be used with looping labeled statements.</dd>
</dl>

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

<p>You can use a label to identify a loop, and then use the <code>break</code> or <code>continue</code> statements to indicate whether a program should interrupt the loop or continue its execution.</p>

<p>Note that JavaScript has <strong>NO</strong>&nbsp;<code>goto</code> statement, you can only use labels with <code>break</code> or <code>continue</code>.</p>

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

<h3 id="Using_a_labeled_continue_with_for_loops">Using a labeled <code>continue</code> with <code>for</code> loops</h3>

<pre class="brush: js">
var i, j;

loop1:
for (i = 0; i &lt; 3; i++) {      //The first for statement is labeled "loop1"
   loop2:
   for (j = 0; j &lt; 3; j++) {   //The second for statement is labeled "loop2"
      if (i == 1 &amp;&amp; j == 1) {
         continue loop1;
      }
      console.log("i = " + i + ", j = " + j);
   }
}

// Output is:
//   "i = 0, j = 0"
//   "i = 0, j = 1"
//   "i = 0, j = 2"
//   "i = 1, j = 0"
//   "i = 2, j = 0"
//   "i = 2, j = 1"
//   "i = 2, j = 2"
// Notice how it skips both "i = 1, j = 1" and "i = 1, j = 2"
</pre>

<h3 id="Using_a_labeled_continue_statement">Using a labeled <code>continue</code> statement</h3>

<p>Given an array of items and an array of tests, this example counts the number of items that passes all the tests.</p>

<pre class="brush: js">
var itemsPassed = 0;
var i, j;

top:
for (i = 0; i &lt; items.length; i++){
  for (j = 0; j &lt; tests.length; j++) {
    if (!tests[j].pass(items[i])) {
      continue top;
    }
  }

  itemsPassed++;
}</pre>

<h3 id="Using_a_labeled_break_with_for_loops">Using a labeled&nbsp;<code>break</code>&nbsp;with&nbsp;<code>for</code>&nbsp;loops</h3>

<pre class="brush: js">
var i, j;

loop1:
for (i = 0; i &lt; 3; i++) {      //The first for statement is labeled "loop1"
   loop2:
   for (j = 0; j &lt; 3; j++) {   //The second for statement is labeled "loop2"
      if (i == 1 &amp;&amp; j == 1) {
         break loop1;
      }
      console.log("i = " + i + ", j = " + j);
   }
}

// Output is:
//   "i = 0, j = 0"
//   "i = 0, j = 1"
//   "i = 0, j = 2"
//   "i = 1, j = 0"
// Notice the difference with the previous continue example</pre>

<h3 id="Using_a_labeled_break_statement">Using a labeled <code>break</code> statement</h3>

<p>Given an array of items and an array of tests, this example determines whether all items pass all tests.</p>

<pre class="brush: js">
var allPass = true;
var i, j;

top:
for (i = 0; items.length; i++)
  for (j = 0; j &lt; tests.length; i++)
    if (!tests[j].pass(items[i])){
      allPass = false;
      break top;
    }</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('ES3')}}</td>
   <td>{{Spec2('ES3')}}</td>
   <td>Initial definition. Implemented in JavaScript 1.2</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-12.12', 'Labelled statement')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-labelled-statements', 'Labelled statement')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-labelled-statements', 'Labelled statement')}}</td>
   <td>{{Spec2('ESDraft')}}</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>{{jsxref("Statements/break", "break")}}</li>
 <li>{{jsxref("Statements/continue", "continue")}}</li>
</ul>
Revert to this revision