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
    Creating Scrolling Parallax Effects with CSS

    Introduction For quite a long time now websites with the so called "parallax" effect have been really popular. In case you have not heard of this effect, it basically includes different layers of images that are moving in different directions or with different speed. This leads to a...

  • By
    Send Text Messages with PHP

    Kids these days, I tell ya.  All they care about is the technology.  The video games.  The bottled water.  Oh, and the texting, always the texting.  Back in my day, all we had was...OK, I had all of these things too.  But I still don't get...

Incredible Demos

  • By
    Create a Photo Stack Effect with Pure CSS Animations or MooTools

    My favorite technological piece of Google Plus is its image upload and display handling.  You can drag the images from your OS right into a browser's DIV element, the images upload right before your eyes, and the albums page displays a sexy photo deck animation...

  • By
    Chris Coyier’s Favorite CodePen Demos IV

    Did you know you can triple-heart things on CodePen? We’ve had that little not-so-hidden feature forever. You can click that little heart button on any Pen (or Project, Collection, or Post) on CodePen to show the creator a little love, but you can click it again...

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!