Course contentsShow
← JavaScript
Lesson 368 of 2,72310. LoopsFree lesson

Continuing to a label: Restarting outer loops

Use continue with a label to skip to the next iteration of a specific outer loop, not just the innermost one.

Continuing to a label: Restarting outer loops

You'll learn how to use continue with a label to skip the current iteration of an outer loop, not just the innermost one.

By default, when you use continue in a nested loop, it only affects the innermost loop—it skips to the next iteration of that loop. But what if you need to skip to the next iteration of an outer loop instead? That's where labeled continue comes in.

The core idea

When you place a label before an outer loop and use continue labelName; inside a nested loop, JavaScript jumps directly to the next iteration of the labeled outer loop, skipping any remaining code in both the inner and outer loops.

How it works

outerLoop: for (let i = 0; i < 3; i++) {
  console.log(`Outer: ${i}`);
  
  for (let j = 0; j < 3; j++) {
    if (j === 1) {
      console.log(`  Skipping rest of outer iteration ${i}`);
      continue outerLoop; // Restarts the outer loop
    }
    console.log(`  Inner: ${j}`);
  }
  
  console.log(`End of outer ${i}`); // Never reached when j === 1
}

Output:

Outer: 0
  Inner: 0
  Skipping rest of outer iteration 0
Outer: 1
  Inner: 0
  Skipping rest of outer iteration 1
Outer: 2
  Inner: 0
  Skipping rest of outer iteration 2

Notice how "End of outer" never prints—the labeled continue immediately jumps to the next value of i, bypassing everything else.

Think of it like nested tasks

Imagine sorting mail into boxes (outer loop) and checking each letter (inner loop). If you find a damaged letter, you might want to stop checking all letters in that box and move to the next box entirely. Labeled continue lets you do exactly that.

Key Takeaway: Use continue with a label to skip to the next iteration of a specific outer loop, bypassing all remaining code in both inner and outer loop bodies.