Definition and Usage
You can place a label (a name used as an identifier) before a statement.
A statement with such a label is called a labeled statement.
If you specify the label of a labeled statement in a break or continue statement, control flow (the location of the code being executed) moves to that labeled statement when the statement is executed.
Before diving into labeled statements, there are a few concepts you should understand first.
- You need to understand how the
breakandcontinuestatements work and in what situations they are used. - You should know that
breakandcontinuestatements can optionally specify a label referencing the execution location.
Prerequisite Knowledge
- You need to understand how the
breakandcontinuestatements work and in what situations they are used.
break Statement
It stops execution of the current loop or block statement, exits the flow, and transfers program control to the next statement.
continue Statement
It stops execution of the current loop, skips the rest of the current iteration, and jumps directly to the next iteration.
What break and continue Statements Have in Common
Both statements share the common behavior of stopping current execution and transferring control.
- You should know that
breakandcontinuestatements can optionally specify a label referencing the execution location.
Specify a Label Referencing the Execution Location
Referencing the execution location means specifying the location where the break or continue statement stops the current execution and moves.
Both break and continue statements can optionally specify a label that references the execution location.
Let's look at where to write the label to be specified in the break and continue statements.
break Statement
// Regular break statement
break;
// break statement with a label
break label;
continue Statement
// Regular continue statement
continue;
// continue statement with a label
continue label;
By specifying a label (label) in a break or continue statement, control flow (the location of the code being executed) moves to the corresponding labeled statement during execution.
Syntax of a Labeled Statement
label: // Labeled statement
statement; // Specific statement corresponding to the execution location
A labeled statement specifies an identifier (label) before a specific statement corresponding to the execution location. A colon (:) is placed immediately after the identifier, forming the pattern label:.
label
Any valid JavaScript identifier can be used.
statement
The statement corresponding to the execution location.
Scope of a Labeled Statement
All break and continue statements that reference a label must be contained within the labeled statement. Think of a label as a variable that can only be used within the scope of a statement.
It cannot be used in an outer statement that is not nested within the labeled statement.
Examples
Labeled statements are mainly used with nested loops.
You can label a loop and use break or continue statements to specify where to stop and resume execution of the loops.
break Statement with a Labeled Statement
/* Without a labeled statement */
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (i === 1 && j === 1) {
break; // Exits only the innermost loop.
}
console.log(`i: ${i}, j: ${j}`);
}
}
/* Output
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
*/
/* With a labeled statement */
outerLoop: for (let k = 0; k < 3; k++) {
for (let l = 0; l < 3; l++) {
if (k === 1 && l === 1) {
break outerLoop; // Exits the loop labeled outerLoop.
}
console.log(`k: ${k}, l: ${l}`);
}
}
/* Output
k: 0, l: 0
k: 0, l: 1
k: 0, l: 2
k: 1, l: 0
*/
continue Statement with a Labeled Statement
/* Without a labeled statement */
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (i === 1 && j === 1) {
console.log("Skipping i: 1, j: 1");
continue; // Moves to the next iteration of the current loop.
}
console.log(`i: ${i}, j: ${j}`);
}
}
/* Output
i: 0, j: 0
i: 0, j: 1
i: 0, j: 2
i: 1, j: 0
Skipping i: 1, j: 1
i: 1, j: 2
i: 2, j: 0
i: 2, j: 1
i: 2, j: 2
*/
/* With a labeled statement */
outerLoop: for (let k = 0; k < 3; k++) {
for (let l = 0; l < 3; l++) {
if (k === 1 && l === 1) {
console.log("Skipping k: 1, l: 1");
continue outerLoop; // Continues to the next iteration of the loop labeled outerLoop.
}
console.log(`k: ${k}, l: ${l}`);
}
}
/* Output
k: 0, l: 0
k: 0, l: 1
k: 0, l: 2
k: 1, l: 0
Skipping k: 1, l: 1
k: 2, l: 0
k: 2, l: 1
k: 2, l: 2
*/
Why Use Labeled Statements?
Labeled statements are often used in nested structures, such as nested loops or conditional statements.
Using labeled statements to clearly control code flow is useful when dealing with complex algorithms or when you need to break or skip loops in specific situations. However, labeled statements can reduce code readability, so they should be used carefully and only when necessary.
Things to Keep in Mind
Labels can also be used with blocks, but labels that are not associated with loops can only be used with break statements.
myBlock: {
console.log("Inside the block.");
break myBlock; // Exits the labeled block.
console.log("This part is not executed.");
}
console.log("Outside the block.");
/* Output:
"Inside the block."
"Outside the block."
*/
A break statement must always be nested within the label it references.
It cannot be used in an outer statement that is not nested within the labeled statement.
myBlock: {
console.log('1');
break outerBlock; // SyntaxError: Undefined label 'outerBlock'
}
outerBlock: {
console.log('2');
}
Compatibility
| Statement |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
Node.js
|
|---|---|---|---|---|---|
label
|
1 | 12 | 1 | 1 | 0.10 |
Specifications
| Specification | |
|---|---|
label
|
ECMAScript® 2027 Language Specification #sec-labelled-statements |