JavaScript: The Label Statement

Explore the label Statement in JavaScript

Brandon Morelli
codeburst
Published in
3 min readFeb 4, 2018

Continue & Break

In my last article, JavaScript: Continue vs Break, we explored the continue and break statements in JavaScript.

As a quick refresher:

  • Continue — The continue statement terminates execution of the current iteration in a loop.
  • Break — The break statement breaks (stops) the execution of a loop entirely.

I strongly encourage you to read my previous article JavaScript: Continue vs Break before continuing.

The Label Statement

In this article, we’ll be diving into the label statement. The label statement allows us to name loops and blocks in JavaScript. We can then use these labels to refer back to the code later.

Consider the following code from the previous article:

for (let i = 1; i <= 10; i++){
console.log(i);
}

Above we have a pretty simple for() loop. Our loop will run 10 different iterations, with the value of i increasing from 1 to 10. Each iteration will print the value of i. If we execute this loop, we get:

1
2
3
4
5
6
7
8
9
10

We can use a label statement to name our loop. We’ll name our loop even:

even:
for (let i = 1; i <= 10; i++){
console.log(i);
}

As you can see, labeling is easy. Immediately before the loop, simply pick a name and add a colon.

We can now refer back to our loop when using continue or break:

But this isn’t anything special — our continue statement would effect the even loop even without the label in this example. Utilizing label statements really shines within nested loops. They allow us to specify exactly which loop we want to break or continue.

Nested For Loops

Take a look at the below example where we use labeled statements in a nested for loop. Try and figure out what will log to the console when you hit run:

Each loop will run three iterations — with values of 0, 1, and 2. Our first loop will alway skip the iteration where i is equal to 1. Therefor, it’s value can only be 0, or 2. Our second loop will always stop when j is 1. Therefor, it’s value can only ever equal 0. Thus we would expect 0 & 0 and 2 & 0 to log to the console.

Labels aren’t common.

Labels are good to know about, but don’t get in the habit of using labels for loops — they’re just not that common.

Closing Notes:

Thanks for reading! If you’re ready to finally learn Web Development, check out: The 2018 Web Developer Roadmap.

If you’re working towards becoming a better JavaScript Developer, check out: Ace Your Javascript Interview — Learn Algorithms + Data Structures.

I publish 4 articles on web development each week. Please consider entering your email here if you’d like to be added to my once-weekly email list, or follow me on Twitter.

If this post was helpful, please click the clap 👏 button below a few times to show your support! ⬇⬇

Published in codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Written by Brandon Morelli

Creator of @codeburstio — Frequently posting web development tutorials & articles. Follow me on Twitter too: @BrandonMorelli

Responses (10)

What are your thoughts?