Revision 65536 of label

  • Revision slug: JavaScript/Reference/Statements/label
  • Revision title: label
  • Revision id: 65536
  • Created:
  • Creator: Ste Al Th
  • Is current revision? No
  • Comment Made an example; 166 words added, 23 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

test1 : for (var i = 0; i < 3; i++) {      //The first for statement is labeled "test1"
   test2 : for (var j = 0; j < 3; j++) {   //The second for statement is labeled "test2"
      if (i == 1 && j == 1) {
         continue test1;
      } else {
         alert("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"*/

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>
<pre>test1 : for (var i = 0; i &lt; 3; i++) {      //The first for statement is labeled "test1"
   test2 : for (var j = 0; j &lt; 3; j++) {   //The second for statement is labeled "test2"
      if (i == 1 &amp;&amp; j == 1) {
         continue test1;
      } else {
         alert("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">See also</h3>
<p><a href="/en/Core_JavaScript_1.5_Reference/Statements/break" title="en/Core_JavaScript_1.5_Reference/Statements/break">break</a>, <a href="/en/Core_JavaScript_1.5_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