{{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, andcontinue
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")}}