toiletpaper-icon-154

Forgotten JavaScript feature: labels

Problem

We may all be familiar with this situation: You have nested loops (or conditions) and need to break out deeply.
Common approaches are e.g.:

  • After the inner loop, exit the outer loop by another condition.
  • Refactoring the code to use an early-return instead of using break.

Solution

In JavaScript, you can solve this differently with an old, largely forgotten feature. Labels make it possible to give loops (and conditions!) a name and then address them with the statements break and continue.

Without labels
for (const row of rows) {
  let breakOuter = false;
  let continueOuter = false;
  for (const cell of row) {
    if (cell === 'break') {
      breakOuter = true;
      break;
    }
    if (cell === 'continue') {
      continueOuter = true;
      break;
    }
  }
  if (breakOuter) {
    break;
  }
  if (continueOuter) {
    continue;
  }
  console.log('-');
}
For loop with labels
outer: for (const row of rows) {
  for (const cell of row) {
    switch (cell) {
      case 'break':
        break outer;
      case 'continue':
        continue outer;
    }
  }
  console.log('-');
}
Conveniently, this also works with conditions:
const [a, b, c] = [1, 3, 2];

found: if (a < b) {
  if (c < b) {
    break found;
  }
  console.log('never gets here');
}
console.log('done');

Example

When should you use labels?

  • JS labels are not the same as GoTo statements. You do not jump to a label but bind a break or continue statement to a condition or loop. Wild jumping like in C/C++ is not possible.
  • As is often the case, it is important to use the right tool at the right time. Sometimes when writing code, there may be better solutions than using labels. But if not, labels are certainly worth considering.
jambit-toiletpaper-154-visual

Further Aspects

---

Author: Santo Pfingsten / Senior Software Architect / Office Leipzig

Cookie Settings

This website uses cookies to personalize content and ads, provide social media features, and analyze website traffic. In addition, information about your use of the website is shared with social media, advertising, and analytics partners. These partners may merge the information with other data that you have provided to them or that they have collected from you using the services.

For more information, please refer to our privacy policy. There you can also change your cookie settings later on.

contact icon

Contact us now