Course contentsShow
← JavaScript
Lesson 330 of 2,7239. Control FlowFree lesson

What Are Labeled Statements?

Understanding label syntax and how labels mark statements for explicit control flow targeting.

What Are Labeled Statements?

You'll learn: How to attach a name to any JavaScript statement so you can target it explicitly with break or continue.

The Core Idea

A labeled statement is simply a statement preceded by an identifier and a colon. It looks like this:

myLabel: statement

The label acts like a nametag you stick on a piece of code. By itself, a label does nothing — it just marks a location. But it becomes powerful when paired with break or continue inside loops, allowing you to jump out of or skip specific nested structures by name instead of just the innermost one.

Think of labels like section markers in a building. Without them, "exit" just means the nearest door. With labels, you can say "exit through the main lobby" even if you're deep inside.

Basic Syntax

outer: for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 3; j++) {
    if (i === 1 && j === 1) {
      break outer; // exits BOTH loops
    }
  }
}

Here, outer: labels the outer loop. The break outer; statement targets that specific loop, not just the inner one.

Why Labels Exist

Without labels, break and continue only affect the immediately enclosing loop. Labels let you control flow across multiple nesting levels, which is occasionally necessary in complex iteration logic.

Common Use

Labels are rare in everyday code but useful for breaking out of deeply nested loops without extra flags or function returns.

Key Takeaway: A labeled statement attaches a name to any statement (usually loops), allowing break or continue to target that specific structure explicitly rather than just the nearest enclosing loop.