Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Statements and Declarations

Labeled statements

Reference for JavaScript labeled statements: name a loop or block, then use break label or continue label to leave or skip an outer loop, with examples.

A label is an identifier followed by a colon placed in front of a statement. On its own a label does nothing. Its job is to give break and continue a target, so that code inside a nested loop can leave or skip an outer loop instead of only the innermost one.

Labels are rarely needed, but when you have two nested loops and want to stop searching as soon as the inner loop finds something, a label is shorter than a flag variable.

JavaScript
outer: for (const a of listA) {
  for (const b of listB) {
    if (a === b) break outer;
  }
}

Try it

JS Leaving two loops at once

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS continue with a label

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
label: statement

break label;
continue label;
Part Description
label Any valid identifier that is not a reserved word. Labels live in their own namespace, so a label may share a name with a variable.
statement Any statement. continue label requires that the labeled statement is a loop; break label works on any labeled statement, including a plain block.

Usage notes

  • break label on a block. A label on a { ... } block lets you jump to its end from inside, even though a block is not a loop. This is handy for an early exit from a long run of checks.
  • continue label only targets loops. It skips the rest of the current iteration of the labeled loop and moves on to its next iteration (running the update expression of a for loop).
  • A label must be visible. You can only break or continue to a label that encloses the statement. You cannot jump sideways or into another function; a function body starts a fresh set of labels.
  • No goto. JavaScript has no general jump. Labels are limited to leaving enclosing statements.
  • Duplicate labels inside each other are a syntax error.
  • Consider alternatives. Extracting the nested loops into a function and using return is often clearer, and Array.prototype.find() or some() express "stop at the first match" without labels.
  • Style. Use short, meaningful names such as outer, search or rows. Many style guides discourage labels except in the cases above.

Browser support

Labeled statements are part of the oldest language specification and work in every engine.