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.
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:
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:
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):
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
breakworks infor,for…of,for…in,while,do…whileandswitch.continueworks in all loops (not inswitchalone).- Neither works inside callbacks like
forEach— there,returnonly ends the current callback. Usefor…of, or methods likesome/findthat stop on their own.
[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?
- Ends the loop
- Skips to the next iteration
- Restarts the loop from the beginning
- Pauses the loop
In nested loops, which loop does a plain break exit?
- All loops
- The outermost loop
- The innermost loop containing it
- None — it only works in switch
How can you stop a forEach early?
- break
- continue
- You can't — use for…of or some/find instead
- return false