The label statement in JavaScript
The label statement gives a name to a block of code or a loop, so that execution can then be steered with break or continue by naming exactly which loop to break out of or continue. It is a rare, rarely used JavaScript tool that makes sense almost exclusively in nested loops.
Theory
TL;DR
- A label is a name placed in front of a block or a loop:
outer: for (...) { ... }. break outerterminates the labelled loop entirely, not just the inner one.continue outerskips the current iteration of the labelled outer loop.- Labels do not create a new scope and are not an equivalent of
goto. - Labelled code usually reads worse, so reach for it only when leaving nested loops is otherwise awkward.
Quick example
outer: for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (i === 1 && j === 1) break outer;
console.log(`i = ${i}, j = ${j}`);
}
}Result:
i = 0, j = 0
i = 0, j = 1
i = 0, j = 2
i = 1, j = 0Syntax
A label can be placed in front of a plain block:
labelName: {
// statements
}But most often it goes in front of a loop:
labelName:
for (let i = 0; i < 5; i++) {
// loop body
}The label name is an ordinary identifier followed by a colon. The label and the loop may sit on the same line or on separate lines; that is a matter of style.
Why a label is needed: leaving nested loops
A label is usually introduced when there are nested loops and you have to leave not only the inner one but the outer one as well. Without labels, break and continue affect only the nearest loop.
Without a label:
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (i === 1 && j === 1) break;
console.log(`i = ${i}, j = ${j}`);
}
}Result:
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 = 2Here break stops only the inner loop, while the outer one (i) happily carries on to i = 2.
With a label:
outerLoop: // a label for the outer loop
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (i === 1 && j === 1) break outerLoop;
console.log(`i = ${i}, j = ${j}`);
}
}Result:
i = 0, j = 0
i = 0, j = 1
i = 0, j = 2
i = 1, j = 0Now break outerLoop terminates the whole outer loop, not just the inner one.
continue with a label
In the same way you can skip an iteration of the outer loop when a condition holds:
outer: for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (j === 2) continue outer; // skips the current i
console.log(`i = ${i}, j = ${j}`);
}
}Result:
i = 0, j = 0
i = 0, j = 1
i = 1, j = 0
i = 1, j = 1
i = 2, j = 0
i = 2, j = 1As soon as j reaches 2, control moves on to the next iteration of the outer loop.
What is important to remember
| What it does | Example |
|---|---|
| Gives a name to a loop or a block | outer: for (...) { ... } |
| Lets you leave the outer loop | break outer; |
| Lets you skip an iteration of the outer loop | continue outer; |
| Creates a new scope | No |
| Use it often? | No, only when necessary |
A few more points:
- Labels create no new scopes; they are just names that
breakandcontinuerefer to. - A label cannot be used for arbitrary jumps the way
gotoworks in other languages (in C, for example). - A labelled
continueonly works with loops, whereas a labelledbreakalso works with a labelled block.
Common mistakes
- Treating a label as
goto. You cannot jump forward or backward to an arbitrary place: you may only leave the labelled construct or move to its next iteration. - Putting a labelled
continueon a plain block. That is a syntax error;continuerequires the label to sit on a loop. - Using a label where a function would do. Extracting the nested loops into a function and using a plain
returnoften reads better thanbreak outer. - Confusing a label with a variable declaration.
outer:is not a variable and cannot be referenced as a value. - Overusing labels. They are rare precisely because they make code harder to follow; take them only when the alternative is genuinely clumsy.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.