# JS Tip of the Day: Labels

**URL:** <https://forum.kirupa.com/t/js-tip-of-the-day-labels/643075>\
**Category:** web dev\
**Created:** [May 22, 2020, 3:12pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-labels/643075 "2020-05-22T15:12:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [May 22, 2020, 3:12pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-labels/643075/1 "2020-05-22T15:12:08Z")

</div>

**Labels**  
Level: Intermediate

Labels are an infrequently used feature of JavaScript. They allow you to name or tag different statements, usually code blocks or loops. When a block is labeled, you can use `break` to break out of it by its label name. With loops, you can also use `continue` to continue a loop by name.

```javascript
myBlockLabel: {
    console.log('Now you see me');
    break myBlockLabel;
    console.log('Now you dont');
}
// Logs: Now you see me

myLoopLabel: for (let i = 0; i < 3; i++) {
    if (i === 1) {
        continue myLoopLabel;
    }
    console.log('Loop: ' + i);
}
/* Logs:
Loop: 0
Loop: 2
*/

```

Labels are really only useful with dealing with nested blocks such as nested loops or nested `switch` statements where in a nested block you might want to break out of an outer block.

```javascript
let flags = [
    [1, 1, 1],
    [1, 0, 1],
    [1, 1, 1]
];
outer: for (let row of flags) {
    for (let flag in row) {
        if (!flag) {
            break outer; // exit this and outer loop
        }
    }
}

```

Normally, breaking inside the inner loop with just a `break` would only break out of the inner loop allowing the outer loop to continue on to the next iteration. To break out of the outer loop too, you’d usually need another `break` and some other flag to know when to use it. With labels, the inner loop can specifically target the outer loop to break out of.

More info:

- [label on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label)
