TExcelTAD
Home › JavaScript › JS Break & Continue

JavaScript Break & Continue

Normally a loop runs until its condition fails. Two statements let you change that from inside the body:

Statement Effect
break leaves the loop immediately
continue skips the rest of the current iteration and moves on to the next

break — stop early

break is perfect for searches: once you've found what you're looking for, there's no reason to keep going.

js · try it yourself
const inventory = ["hammer", "saw", "drill", "wrench", "pliers"];
let position = -1;

for (let i = 0; i < inventory.length; i++) {
  console.log("checking", inventory[i]);
  if (inventory[i] === "drill") {
    position = i;
    break;
  }
}
console.log("drill found at", position);
checking hammer
checking saw
checking drill
drill found at 2

It also gives you an exit from deliberately endless loops:

js · try it yourself
let n = 1;
while (true) {
  n *= 3;
  if (n > 500) break;
}
console.log(n);
729

continue — skip an iteration

continue jumps straight to the next iteration (in a for loop, the update expression still runs). It's a clean way to filter out items you don't want to process:

js · try it yourself
const entries = ["42", "", "17", "abc", "8"];
let sum = 0;

for (const entry of entries) {
  if (entry === "") continue;          // skip blanks
  const value = Number(entry);
  if (Number.isNaN(value)) {
    console.log(`skipping invalid "${entry}"`);
    continue;
  }
  sum += value;
}
console.log("sum:", sum);
skipping invalid "abc"
sum: 67

Labels: breaking out of nested loops

A plain break only exits the innermost loop. To leave an outer loop from inside an inner one, put a label before the outer loop and name it in the break (or continue):

js · try it yourself
const grid = [
  [3, 8, 1],
  [9, 4, 7],
  [2, 6, 5],
];

let found = null;
search: for (let r = 0; r < grid.length; r++) {
  for (let c = 0; c < grid[r].length; c++) {
    if (grid[r][c] === 7) {
      found = [r, c];
      break search;          // exits both loops
    }
  }
}
console.log("7 is at", found);

outer: for (let i = 1; i <= 3; i++) {
  for (let j = 1; j <= 3; j++) {
    if (j === 2) continue outer;   // skip to the next i
    console.log(i, j);
  }
}
7 is at [ 1, 2 ]
1 1
2 1
3 1

Labels are rare in everyday code. Often, moving the nested loops into a function and using return is clearer.

Where they work

  • break works in for, for…of, for…in, while, do…while and switch.
  • continue works in all loops (not in switch alone).
  • Neither works inside callbacks like forEach — there, return only ends the current callback. Use for…of, or methods like some/find that stop on their own.
js · try it yourself
[1, 2, 3, 4].forEach((n) => {
  if (n === 2) return;   // acts like continue, not break
  console.log("forEach", n);
});

[1, 2, 3, 4].some((n) => {
  console.log("some", n);
  return n === 2;        // returning true stops the loop
});
forEach 1
forEach 3
forEach 4
some 1
some 2

Test yourself

What does continue do inside a loop?

  1. Ends the loop
  2. Skips to the next iteration
  3. Restarts the loop from the beginning
  4. Pauses the loop

In nested loops, which loop does a plain break exit?

  1. All loops
  2. The outermost loop
  3. The innermost loop containing it
  4. None — it only works in switch

How can you stop a forEach early?

  1. break
  2. continue
  3. You can't — use for…of or some/find instead
  4. return false