JavaScript Tutorial
JavaScript Break and Continue
break leaves the loop now. continue skips the rest of this round and starts the next one.
Two ways to change a loop
A loop normally runs until its test is false. Sometimes you want to stop early. Sometimes you want to skip one round and keep going. That is what break andcontinue are for.
Both work in for, while, and do...while. They also work in switch — break there leaves the switch, not a loop.
break leaves now
When the engine hits break, it jumps to the first statement after the loop. The remaining rounds never run. Use it when you already have the answer: a match, a stop value, a found item.
Example
const scores = [12, 18, 9, 21, 7];
let found = null;
for (let i = 0; i < scores.length; i++) {
if (scores[i] > 20) {
found = scores[i];
break;
}
}
console.log(found);
The loop would have checked 7 as well. After 21 it stops, sofound is 21 and the last item is never read.
Click Try it in JavaScript under an example. That opens/javascript/try — a live page and a console.
continue skips this round
continue does not leave the loop. It jumps to the next round: the step in afor, or the test in a while. Code after continue in that same round does not run.
Example
const nums = [1, 2, 3, 4, 5, 6];
const odds = [];
for (const n of nums) {
if (n % 2 === 0) {
continue;
}
odds.push(n);
}
console.log(odds);
Even numbers hit continue and skip push. The result is[1, 3, 5]. A filter would do the same job; continue is useful when the body is longer than one line.
break versus continue
| break | continue | |
|---|---|---|
| Leaves the loop | Yes | No |
| Skips the rest of this round | Yes — and every later round | Yes — only this round |
| Typical use | Stop when you have enough | Skip a bad or unneeded item |
If you mix them up, you either stop too soon or keep looping after you already have the result. Read the test next to the keyword: “leave now” or “try the next one.”
while and an early stop
A while with break is a search that can finish the moment it succeeds. Without break you would keep walking after the match.
Example
const names = ["Ada", "Lin", "Grace", "Alan"];
let i = 0;
let match = null;
while (i < names.length) {
if (names[i] === "Grace") {
match = names[i];
break;
}
i += 1;
}
console.log(match);
console.log("Stopped at index " + i);
Nested loops
break and continue apply to the innermost loop that contains them. Breaking the inner loop does not stop the outer one. Labels exist for breaking an outer loop, but you rarely need them at this stage — a flag or a functionreturn is clearer.
Example
const grid = [
[1, 2, 3],
[4, 9, 6],
[7, 8, 0],
];
for (let r = 0; r < grid.length; r++) {
for (let c = 0; c < grid[r].length; c++) {
if (grid[r][c] === 9) {
console.log("found at", r, c);
break;
}
}
}
After printing found at 1 1, the inner loop stops. The outer loop still runs row 2. If you need to stop both, put the search in a function andreturn from it.
What to remember
breakexits the loop immediately.continueskips the rest of the current round.- Both affect the innermost loop unless you use a label.
- In a
switch,breakends the switch, not a surrounding loop.
Next: Sets — a collection that throws duplicates away so you do not need a loop to unique a list by hand.