label

  • Revision slug: JavaScript/Reference/Statements/label
  • Revision title: label
  • Revision id: 235901
  • Created:
  • Creator: dbruant
  • Is current revision? No
  • Comment 59 words added, 6 words removed

Revision Content

Summary

Provides a statement with an identifier that you can refer to using a break or continue statement.

For example, 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.

Statement
Implemented in: JavaScript 1.2, NES 3.0
ECMA Version: ECMA-262, Edition 3

Syntax

label :
   statement

Parameters

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.

Avoid using labels

Labels are not very commonly used in JavaScript since they make programs harder to read an understand. As much as possible, avoid using labels and, depending on the cases, prefer calling functions or throwing an error.

Examples

continue Example

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;
      } else {
         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"

Example 2 (continue)

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++;
}

Example 3 (break)

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;
    }

See also

break, continue

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

Revision Source

<h2 name="Summary">Summary</h2>
<p>Provides a statement with an identifier that you can refer to using a <code>break</code> or <code>continue</code> statement.</p>
<p>For example, 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>
<table class="fullwidth-table"> <tbody> <tr> <td class="header" colspan="2">Statement</td> </tr> <tr> <td>Implemented in:</td> <td>JavaScript 1.2, NES 3.0</td> </tr> <tr> <td>ECMA Version:</td> <td>ECMA-262, Edition 3</td> </tr> </tbody>
</table>
<h2 name="Syntax">Syntax</h2>
<pre class="eval"><em>label</em> :
   <em>statement</em>
</pre>
<h2 name="Parameters">Parameters</h2>
<dl> <dt><code>label</code></dt> <dd>Any JavaScript identifier that is not a reserved word.</dd>
</dl>
<dl> <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>Avoid using labels</h2>
<p>Labels are not very commonly used in JavaScript since they make programs harder to read an understand. As much as possible, avoid using labels and, depending on the cases, prefer <a href="/en/JavaScript/Reference/Statements/function" title="function">calling functions</a> or <a href="/en/JavaScript/Reference/Statements/throw" title="throw">throwing</a> an error.</p>
<h2 name="Examples">Examples</h2>
<h3><code>continue</code> Example</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;
      } else {
         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 name="See_also">Example 2 <code>(continue)</code></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>Example 3 <code>(break)</code></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>See also</h2>
<p><a href="/en/JavaScript/Reference/Statements/break" title="en/Core_JavaScript_1.5_Reference/Statements/break">break</a>, <a href="/en/JavaScript/Reference/Statements/continue" title="en/Core_JavaScript_1.5_Reference/Statements/continue">continue</a></p>
<p>{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Sentencias/label", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Instructions/label", "ja": "ja/Core_JavaScript_1.5_Reference/Statements/label", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Polecenia/etykieta" } ) }}</p>
Revert to this revision