In short
Use break to stop a JavaScript loop early and continue to skip one round and move to the next. This lesson shows both with simple, real examples. How to stop a loop early with break Sometimes you find what you need before the loop is finished. Imagine looking for your keys in five drawers.
Use break to stop a JavaScript loop early and continue to skip one round and move to the next. This lesson shows both with simple, real examples.
How to stop a loop early with break
Sometimes you find what you need before the loop is finished. Imagine looking for your keys in five drawers. If you find them in drawer two, you stop. You do not open the other three.
The break keyword stops the loop immediately. JavaScript jumps to the first line after the loop.
const scores = [45, 72, 38, 91, 60];
for (let i = 0; i < scores.length; i++) {
console.log(`Checking score ${scores[i]}...`);
if (scores[i] > 90) {
console.log(`Found a top score: ${scores[i]} at position ${i}`);
break;
}
}
// Output:
// Checking score 45...
// Checking score 72...
// Checking score 38...
// Checking score 91...
// Found a top score: 91 at position 3
The loop checks each score. When it reaches 91, the if condition is true, so we print a message and break. Score 60 is never checked, which saves work.
break also works in while loops. It is useful for loops that could run "forever" but have a clear exit inside:
let attempts = 0;
while (true) {
attempts++;
const roll = Math.floor(Math.random() * 6) + 1;
console.log(`Roll ${attempts}: ${roll}`);
if (roll === 6) {
console.log(`You got a 6 after ${attempts} rolls!`);
break;
}
}
// Output (random, for example):
// Roll 1: 3
// Roll 2: 6
// You got a 6 after 2 rolls!
while (true) would normally never end, but break exits as soon as we roll a 6. Math.random() gives a random decimal between 0 and 1. Multiplying by 6, rounding down with Math.floor() and adding 1 gives a whole number from 1 to 6, like a dice.
Note: You saw
breakbefore in the switch lesson. There it stops the switch. Inside a loop, it stops the loop.
How to skip an item with continue
The continue keyword does not stop the whole loop. It skips the rest of the current round and jumps to the next one. Think of a cashier who skips an expired coupon but keeps scanning the rest.
const prices = [12, -5, 30, 0, 8];
let total = 0;
for (let i = 0; i < prices.length; i++) {
if (prices[i] <= 0) {
console.log(`Skipping invalid price: ${prices[i]}`);
continue;
}
total += prices[i];
}
console.log(`Total: $${total}`);
// Output:
// Skipping invalid price: -5
// Skipping invalid price: 0
// Total: $50
When a price is zero or negative, continue jumps straight to the next round, so total += prices[i] does not run for that item. Only 12, 30 and 8 are added.
Common mistake: Using
continuein a while loop before the line that changes the counter. The counter never updates, and the loop runs forever. In while loops, update the counter before anycontinue.
What is the difference between break and continue?
| Keyword | What it does | Real-life comparison |
|---|---|---|
break | Stops the whole loop now | You found your keys, stop searching |
continue | Skips this round, goes to the next | Skip a bad apple, keep sorting the basket |
Both are optional. If your loop becomes hard to read because of many break and continue lines, a clearer condition is often better.
A short note on nested loops and labels
A nested loop is a loop inside another loop. A break only stops the loop it is directly inside, not the outer one. If you need to stop both, you can give the outer loop a label (a name followed by a colon) and write break outer;.
outer: for (let row = 1; row <= 3; row++) {
for (let seat = 1; seat <= 3; seat++) {
if (row === 2 && seat === 2) {
console.log(`Seat found: row ${row}, seat ${seat}`);
break outer;
}
}
}
// Output: Seat found: row 2, seat 2
Labels are rare in everyday code. Once you learn functions, you will usually use return to exit instead, which is easier to read.
Practice exercise
Loop through the numbers 1 to 50. Skip every number that is divisible by 3 using continue, print the others, and stop the loop completely with break as soon as you reach a number greater than 40.
Hint: i % 3 === 0 means the number is divisible by 3. Put the break check first in the loop body, then the continue check, then console.log(i).
Key takeaways
breakstops a loop immediately and moves to the code after it.continueskips the rest of the current round and starts the next one.- Both work in for, while and do...while loops.
breakonly exits the innermost loop; a label can exit an outer loop, but it is rarely needed.