Yes, JavaScript Has Labels (and They Fix Nested Loop Escapes)
This one gets a "wait, that's legal?" almost every time it comes up in code review. JavaScript loops can be named, and break and continue can target those names:
search:
for (const row of grid) {
for (const cell of row) {
if (cell.value === target) {
found = cell;
break search; // exits BOTH loops
}
}
}
Without the label, break only exits the inner loop and you end up with the classic flag dance: set let done = true, break, then check the flag in the outer loop's condition. The label says exactly what you mean instead.
continue works with labels too
Targeting continue at an outer loop means "skip to the next outer iteration", which reads much better than restructuring conditions:
rows:
for (const row of importedRows) {
for (const validator of validators) {
if (!validator(row)) {
skipped.push(row);
continue rows; // abandon this row, move to the next one
}
}
save(row); // only runs if every validator passed
}
This validation-pipeline shape is where I actually use labels in real code: CSV imports, matrix scans, collision checks - anywhere a nested search wants to bail early.
The fine print
- Labels only work with break and continue. There is no goto in JavaScript, and a label alone does nothing.
- Labels can name any statement for break (even a plain block), but continue requires a loop. Breaking out of a labeled block is legal and occasionally handy, though at that point a function usually reads better.
- Minifiers handle them fine; they're standard syntax from ES1, so support concerns simply don't exist.
When not to use them
If the nested loop body is long enough that the label and the break are far apart, extract the inner search into a function and use return instead - a return is the clearest early exit there is. Labels earn their keep in short, dense loops where a function boundary would just add ceremony.
It's a forty-year-old idea from C that JavaScript quietly kept. Once you know it exists, you'll spot at least one flag variable in your codebase that deserves to be deleted.
Full-stack web developer sharing practical tutorials and building tools that ship.
Got something on your mind?
My inbox is open - no forms disappearing into the void here.
- Just say hello Found a tutorial useful? Spotted a mistake? Tell me.
- Hire me for a project Have something custom in mind? Let's talk scope and timelines.
- Product support Bought something here? I'll help you get it running.
I usually reply within 1-2 business days.