JavaScript ยท Chapter 34 of 55
JavaScript Break & Continue
The `break` statement immediately exits a loop (or switch), while `continue` skips the rest of the current iteration and moves to the next one.
Both are useful for controlling loop flow precisely, such as stopping a search early once a match is found, or skipping invalid items.
break
`break` stops the loop entirely, jumping to the code right after the loop. It's often used once a condition (like a found item) is met.
continue
`continue` skips just the current iteration, jumping straight to the next one without exiting the loop.
Example 1 (javascript)
for (let i = 0; i < 10; i++) {
if (i === 3) break;
console.log(i);
}Output
0
1
2The loop exits entirely when i equals 3.
Example 2 (javascript)
for (let i = 0; i < 5; i++) {
if (i % 2 === 0) continue;
console.log(i);
}Output
1
3continue skips even numbers, only odd ones get logged.
Key points
- break exits the loop entirely.
- continue skips to the next iteration.
- Both work in for, while, and do...while loops.
- break also exits switch statements.
๐ก Note: Overusing break/continue can hurt readability โ sometimes restructuring the condition is clearer.
