JavaScript Break and Continue

The break and continue statements give you control over loop execution. break exits the loop entirely, while continue skips the current iteration and moves to the next one.

The break Statement

The break statement terminates the current loop immediately. Execution continues with the first statement after the loop.

break Statement
// Exit loop when value is found
for (let i = 0; i < 10; i++) {
  if (i === 5) {
    console.log("Found 5, stopping!");
    break;
  }
  console.log(i);
}
// Output: 0, 1, 2, 3, 4, "Found 5, stopping!"

// Search in array
let numbers = [10, 20, 30, 40, 50];
let target = 30;
let found = false;

for (let i = 0; i < numbers.length; i++) {
  if (numbers[i] === target) {
    found = true;
    console.log("Found " + target + " at index " + i);
    break;
  }
}
// Output: "Found 30 at index 2"

The continue Statement

The continue statement skips the rest of the current iteration and jumps to the next one. The loop continues running.

continue Statement
// Skip even numbers
for (let i = 0; i < 10; i++) {
  if (i % 2 === 0) {
    continue; // Skip even numbers
  }
  console.log(i);
}
// Output: 1, 3, 5, 7, 9

// Skip specific items
let fruits = ["apple", "", "banana", null, "cherry"];
for (let fruit of fruits) {
  if (!fruit) {
    continue; // Skip empty/null values
  }
  console.log(fruit);
}
// Output: apple, banana, cherry

break vs continue

The key difference: break exits the entire loop, while continue only skips the current iteration. Choose based on whether you want to stop or skip.

Featurebreakcontinue
EffectExits the loop entirelySkips to next iteration
Code after itNot executed (exits loop)Not executed (this iteration)
Loop continues?NoYes
Use caseStop searching after findingSkip unwanted items
break vs continue Side by Side
console.log("--- Using break ---");
for (let i = 1; i <= 5; i++) {
  if (i === 3) break;
  console.log(i);
}
// Output: 1, 2

console.log("--- Using continue ---");
for (let i = 1; i <= 5; i++) {
  if (i === 3) continue;
  console.log(i);
}
// Output: 1, 2, 4, 5

Labeled Statements

Labels allow you to break or continue outer loops from inside nested loops. A label is an identifier followed by a colon before a loop statement.

Labeled break and continue
// Labeled break: exit outer loop
outer: for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 3; j++) {
    if (i === 1 && j === 1) {
      console.log("Breaking outer loop");
      break outer;
    }
    console.log("i=" + i + ", j=" + j);
  }
}
// Output: i=0,j=0  i=0,j=1  i=0,j=2  i=1,j=0  Breaking outer loop

// Labeled continue: skip to next outer iteration
outer2: for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 3; j++) {
    if (j === 1) {
      continue outer2;
    }
    console.log("i=" + i + ", j=" + j);
  }
}
// Output: i=0,j=0  i=1,j=0  i=2,j=0

break in Nested Loops

Without labels, break only exits the innermost loop. To exit multiple levels, use labels or restructure your code using functions with return.

Nested Loop Patterns
// Without label: break only exits inner loop
for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 3; j++) {
    if (j === 1) break; // Only breaks inner loop
    console.log("i=" + i + ", j=" + j);
  }
}
// Output: i=0,j=0  i=1,j=0  i=2,j=0

// Alternative: use a function with return
function findValue(matrix, target) {
  for (let i = 0; i < matrix.length; i++) {
    for (let j = 0; j < matrix[i].length; j++) {
      if (matrix[i][j] === target) {
        return { row: i, col: j }; // Exits both loops
      }
    }
  }
  return null;
}

let grid = [[1, 2], [3, 4], [5, 6]];
console.log(findValue(grid, 4)); // { row: 1, col: 1 }
📝 Note: Labels are rarely needed in practice. If you find yourself using labeled breaks frequently, consider refactoring the nested loops into separate functions and using return instead.
Exercise:
What happens when continue is executed inside a for loop?
Try it YourselfCtrl+Enter to run
Click Run to see the output here.