Break and Continue in JavaScript
The break and continue statements are used to control the flow of loops in JavaScript.
🚫 1. break Statement
The break statement immediately exits the loop or switch block it's in.
for (let i = 1; i <= 5; i++) {
if (i === 3) {
break;
}
console.log(i);
}
Output will be: 1, 2. The loop stops when i === 3.
⏭️ 2. continue Statement
The continue statement skips the current iteration and moves to the next one.
for (let i = 1; i <= 5; i++) {
if (i === 3) {
continue;
}
console.log(i);
}
Output will be: 1, 2, 4, 5. The number 3 is skipped.
🎯 Use Cases
- break – Exit the loop early when a condition is met
- continue – Skip an iteration and move to the next
💡 Tip: Use
continue carefully to avoid skipping important logic. Always test your loop output.