Web Advent Calendar 2024

JavaScript Labeled statement

Loop statements in JavaScript can be labeled. This way, cou can continue or break out of a specific loop, and not only the one you are actually inside.

// The first for statement is labeled "loop1"
loop1: for (let i = 0; i < 3; i++) {
  // The second for statement is labeled "loop2"
  loop2: for (let j = 0; j < 3; j++) {
    if (i === 1 && j === 1) {
      continue loop1;
    }
    console.log(`i = ${i}, j = ${j}`);
  }
}

This principle also works for break inside block statements:

foo: {
  console.log("wurst");
  break foo;
  console.log("this will not be executed");
}
console.log("brot");

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label