label

  • Revision slug: JavaScript/Reference/Statements/label
  • Revision title: label
  • Revision id: 65540
  • Created:
  • Creator: Trinithis
  • Is current revision? No
  • Comment 10 words added, 2 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.

Examples

continue Example

loop1:
for (var i = 0; i < 3; i++) {      //The first for statement is labeled "loop1"
   loop2:
   for (var 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;
top:
for (var i = 0; i < items.length; i++){
    for (var 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;
top:
for (var i = 0; items.length; i++)
    for (var 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

<p> </p>
<h3 name="Summary">Summary</h3>
<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>
<h3 name="Syntax">Syntax</h3>
<pre class="eval"><em>label</em> :
   <em>statement</em>
</pre>
<h3 name="Parameters">Parameters</h3>
<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>
<h3 name="Examples">Examples</h3>
<h4><code>continue</code> Example</h4>
<pre>loop1:
for (var i = 0; i &lt; 3; i++) {      //The first for statement is labeled "loop1"
   loop2:
   for (var 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>
<h4 name="See_also">Example 2 <code>(continue)</code></h4>
<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>var itemsPassed = 0;
top:
for (var i = 0; i &lt; items.length; i++){
    for (var j = 0; j &lt; tests.length; j++)
        if (!tests[j].pass(items[i]))
            continue top;
    itemsPassed++;
}
</pre>
<h4>Example 3 <code>(break)</code></h4>
<p>Given an array of items and an array of tests, this example determines whether all items pass all tests.</p>
<pre>var allPass = true;
top:
for (var i = 0; items.length; i++)
    for (var j = 0; j &lt; tests.length; i++)
        if (!tests[j].pass(items[i])){
            allPass = false;
            break top;
        }
</pre>
<p>See also</p>
<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> </p>
<p> </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