Skip to main content

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 outer terminates the labelled loop entirely, not just the inner one.
  • continue outer skips 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

javascript
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 = 0

Syntax

A label can be placed in front of a plain block:

javascript
labelName: { // statements }

But most often it goes in front of a loop:

javascript
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:

javascript
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 = 2

Here break stops only the inner loop, while the outer one (i) happily carries on to i = 2.

With a label:

javascript
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 = 0

Now 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:

javascript
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 = 1

As soon as j reaches 2, control moves on to the next iteration of the outer loop.

What is important to remember

What it doesExample
Gives a name to a loop or a blockouter: for (...) { ... }
Lets you leave the outer loopbreak outer;
Lets you skip an iteration of the outer loopcontinue outer;
Creates a new scopeNo
Use it often?No, only when necessary

A few more points:

  • Labels create no new scopes; they are just names that break and continue refer to.
  • A label cannot be used for arbitrary jumps the way goto works in other languages (in C, for example).
  • A labelled continue only works with loops, whereas a labelled break also 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 continue on a plain block. That is a syntax error; continue requires 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 return often reads better than break 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 ready
Premium

A concise answer to help you respond confidently on this topic during an interview.