Labeled Statements in JavaScript Nested Loops

Labeled statements in JavaScript provide a mechanism to name a loop or block of code, allowing break and continue statements to target an outer loop rather than just the immediate enclosing loop. In nested loop structures, this feature gives developers precise control over the flow of execution, enabling early exits or skips across multiple levels of nesting without relying on complex boolean flags.

The Problem with Standard Nested Loops

By default, the break and continue keywords in JavaScript only affect the innermost loop that directly contains them.

When searching through a multi-dimensional array or performing a matrix-style operation, you often need to stop all iterations completely once a condition is met. Without labeled statements, terminating the outer loop from an inner loop requires setting a flag variable and checking that flag on every subsequent iteration of the outer loop:

let found = false;

for (let i = 0; i < matrix.length; i++) {
  for (let j = 0; j < matrix[i].length; j++) {
    if (matrix[i][j] === target) {
      found = true;
      break; // Only exits the inner loop
    }
  }
  if (found) {
    break; // Exits the outer loop
  }
}

How Labeled Statements Work

A label is simply an identifier followed by a colon placed immediately before a loop statement. You can then reference this identifier with break or continue.

Syntax

outerLoop: for (let i = 0; i < 5; i++) {
  innerLoop: for (let j = 0; j < 5; j++) {
    // control statements can target outerLoop or innerLoop
  }
}

Breaking Out of an Outer Loop

Using break labelName immediately terminates execution of the labeled loop, bypassing any remaining iterations of both the inner and outer loops:

outerLoop: for (let i = 0; i < matrix.length; i++) {
  for (let j = 0; j < matrix[i].length; j++) {
    if (matrix[i][j] === target) {
      console.log(`Found ${target} at [${i}, ${j}]`);
      break outerLoop; // Terminates both loops immediately
    }
  }
}

Continuing to the Next Iteration of an Outer Loop

Using continue labelName skips the rest of the inner loop and immediately jumps to the next iteration of the labeled outer loop:

rowLoop: for (let row = 0; row < grid.length; row++) {
  for (let col = 0; col < grid[row].length; col++) {
    if (grid[row][col] === null) {
      // Row is invalid; skip the rest of this row immediately
      continue rowLoop;
    }
    processCell(grid[row][col]);
  }
}

Primary Use Cases

Best Practices

While labeled statements are powerful for nested iteration, use them judiciously. Overusing labels across deeply nested structures can make control flow harder to follow. For complex data processing, consider refactoring nested loops into helper functions where a simple return statement can exit the execution context cleanly.