Labels have been part of JavaScript from the language's beginning. They are not well known, and they can be confusing, but in a narrow set of situations they do real work. One point of confusion to clear up first: a JavaScript label has nothing to do with the HTML <label> element.

What labels actually are

A label names a statement or block of code — most often a loop or a conditional. Naming a statement lets you target it with break or continue from somewhere inside it. The syntax is simply label: placed before the statement, and the identifier you use there is the one you reference later.

let x = 0;
// Label a loop as "myLoop"
myLoop:
while (true) {
  if (x >= 10) {
    // This will cause "myLoop" to end.
    break myLoop;
  }
  x++;
}

Labels are an internal reference only. You cannot look them up, export them, or store one in a value. They also live separately from variable names, so naming a variable the same as a loop label is legal — which is exactly why you shouldn't do it.

Refresher on break and continue

break terminates the loop or conditional currently running. You see it most in switch statements to close a case, but it also stops an if statement early or halts a for or while loop outright.

const x = 1;
switch(x) {
  case 1:
    console.log('On your mark!');
    break; // Doesn't check the rest of the switch statement if 1 is true
  case 2:
    console.log('Get set!');
    break; // Doesn't check the rest of the switch statement if 2 is true
  case 3:
    console.log('GO!');
    break; // Doesn't check the rest of the switch statement if 3 is true
}
// logs: 'On your mark!'

continue is loop-only and does something different: it ends the current iteration early and moves the loop on to its next run.

for (let x = 0; x &< 10; x++) {
  if (x !== 5) continue; // If the number isn't five, go to the next pass of the loop.
  console.log(x);
}
// logs: 5

Jumping out of conditional statements

Labeled break is most useful once statements are nested. Pointed at a label, it immediately terminates even a deeply nested loop or conditional.

// Our outer if statement
outerIf: 
if (true) {
  // Our inner if statement
  innerIf:
  if (true) {
    break outerIf; // Immediately skips to the end of the outer if statement
  }
  console.log('This never logs!');
}

That includes an if statement. It can be labeled like any other statement.

Jumping across loop levels

The other standard case for labels is a nested loop where you want the outer loop to skip ahead while you are inside the inner loop. The unlabeled approach means breaking the inner loop, testing whether you are in the state you wanted to skip, then calling continue on the outer loop. A label makes that intent a single readable statement.

let x = 0;
outerLoop:
while (x < 10) {
  x++;
  for (let y = 0; y < x; y++) {
    // This will jump back to the top of outerLoop
    if (y === 5) continue outerLoop;
    console.log(x,y);
  }
  console.log('----'); // This will only happen if x < 6
}

Block statements and one production-shaped example

Block statements scope const and let to just a portion of your code, which is handy for localizing variables without writing a function. They can be labeled too.

// This example throws a syntax error in an ES module
const myElement = document.createElement('p');
myConditionalBlock: {
  const myHash = window.location.hash;
  // escape the block if there is not a hash.
  if (!myHash) break myConditionalBlock;
  myElement.innerText = myHash;
}
console.log(myHash); // undefined
document.body.appendChild(myElement);

Note the big caveat: block statements are invalid in strict mode, which is the default for ES modules.

Production code rarely needs labels, but one plausible use is escaping a loop from inside a switch statement — the loop terminating as soon as a condition matches instead of running on. A calculator app is a workable illustration:

const calculatorActions = [
  { action: "ADD", amount: 1 },
  { action: "SUB", amount: 5 },
  { action: "EQ" },
  { action: "ADD", amount: 10 }
];
    
let el = {};
let amount = 0;
calculate: while (el) {
  // Remove the first element of the calculatorActions array
  el = calculatorActions.shift();
  switch (el.action) {
    case "ADD":
      amount += el.amount;
      break; // Breaks the switch
    case "SUB":
      amount -= el.amount;
      break; // Breaks the switch
    case "EQ":
      break calculate; // Breaks the loop
    default:
      continue calculate; // If we have an action we don't know, skip it.
  }
}

Here, once the condition matches, the labeled break exits the calculate loop anyway, rather than allowing the script to continue.

Most developers will get through an entire career without needing any of this. The point of knowing the syntax is the odd case where it is the clearest tool available.