label

Names a loop or block so break and continue can target it.

Since ES1 Spec ↗

Syntax

labelName:
statement

Examples

outer:
for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 3; j++) {
    if (i + j === 2) break outer;
    console.log(i, j);
  }
}
Output
0 0
0 1
1 0
loop:
for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 3; j++) {
    if (j === 1) continue loop;
    console.log(i, j);
  }
}
Output
0 0
1 0
2 0
block: {
  console.log("before");
  break block;
  console.log("after");
}
Output
before

Notes

- Labels are most useful for breaking or continuing an outer loop from a nested loop. - A labeled block supports `break` (but not `continue`). - Overuse of labels can hurt readability; prefer extracting functions.

Browser & runtime support

EnvironmentSince version
chrome 1.0
firefox 1.0
safari 1.0
edge 12
node 0.10

See also