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
    Create a Sheen Logo Effect with CSS

    I was inspired when I first saw Addy Osmani's original ShineTime blog post.  The hover sheen effect is simple but awesome.  When I started my blog redesign, I really wanted to use a sheen effect with my logo.  Using two HTML elements and...

  • By
    7 Essential JavaScript Functions

    I remember the early days of JavaScript where you needed a simple function for just about everything because the browser vendors implemented features differently, and not just edge features, basic features, like addEventListener and attachEvent.  Times have changed but there are still a few functions each developer should...

Incredible Demos

  • By
    PHP Woot Checker &#8211; Tech, Wine, and Shirt Woot

    If you haven't heard of Woot.com, you've been living under a rock. For those who have been under the proverbial rock, here's the plot: Every day, Woot sells one product. Once the item is sold out, no more items are available for purchase. You don't know how many...

  • By
    Scrolling &#8220;Agree to Terms&#8221; Component with MooTools ScrollSpy

    Remember the good old days of Windows applications forcing you to scroll down to the bottom of the "terms and conditions" pane, theoretically in an effort ensure that you actually read them? You're saying "No David, don't do it." Too late -- I've done...

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!