JavaScript Labels

By  on  

No matter how long you've been a JavaScript developer, there will always be language features that you didn't know about until you saw them in a fringe piece of code. Your reaction generally is a bit like:

Thinking

One of those features I see developers quizically trying to figure out is JavaScript labels, which allow you to manage break and continue with JavaScript loops. Let's have a look at them!

The basic format of a loop is {loopname}: before the loop starts:

{loopName}:
for({iterating}) {
  {actions}
}

The power of labels comes with nested loops -- you can use break and continue, paired with the label name, to manage loop escaping:

function gogogo() {
  firstLoop:
  for (let outer = 0; outer < 4; outer++) {
    secondLoop:
    for (let inner = 0; inner < 5; inner++) {
      if (inner === 3) {
        // Use continue to avoid runs 4 and 5
        continue firstLoop;
      }
      console.warn(`outer: ${outer}; inner: ${inner}`);
    }
  }
}

/*
outer: 0; inner: 0
outer: 0; inner: 1
outer: 0; inner: 2
outer: 1; inner: 0
outer: 1; inner: 1
outer: 1; inner: 2
outer: 2; inner: 0
outer: 2; inner: 1
outer: 2; inner: 2
outer: 3; inner: 0
outer: 3; inner: 1
outer: 3; inner: 2
*/

Nested loops can be difficult to manage but labels make directing and escaping them easy. The next time you want to look like a smart one in the room, break out the JavaScript labels!

Recent Features

  • By
    Being a Dev Dad

    I get asked loads of questions every day but I'm always surprised that they're rarely questions about code or even tech -- many of the questions I get are more about non-dev stuff like what my office is like, what software I use, and oftentimes...

  • By
    I&#8217;m an Impostor

    This is the hardest thing I've ever had to write, much less admit to myself.  I've written resignation letters from jobs I've loved, I've ended relationships, I've failed at a host of tasks, and let myself down in my life.  All of those feelings were very...

Incredible Demos

  • By
    Adding Events to Adding Events in MooTools

    Note: This post has been updated. One of my huge web peeves is when an element has click events attached to it but the element doesn't sport the "pointer" cursor. I mean how the hell is the user supposed to know they can/should click on...

  • By
    Send Email Notifications for Broken Images Using MooTools AJAX

    One of the little known JavaScript events is the image onError event. This event is triggered when an image 404's out because it doesn't exist. Broken images can make your website look unprofessional and it's important to fix broken images as soon as possible.

Discussion

  1. Honestly, I am using JS since 2012 but today for the first time I’ve seen this feature. Also, as per MDN “Starting with ECMAScript 2015, labeled function declarations are now standardized for non-strict code.”

  2. The output of the example would be the same if “continue firstLoop” were replaced by “break” and the labels were omitted. Perhaps you meant to write “break firstLoop” rather then “continue firstLoop”, in which case the output would end after three lines. I suspect that such breaking out of inner loops is the main use case for labels in JavaScript.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!