continue skips the rest of the current iteration and jumps straight to the next one. Where break says "stop the loop", continue says "skip this one, keep going".
for (let i = 1; i <= 5; i++) {
if (i === 3) {
continue;
}
console.log(i);
}
When i is 3, continue skips the console.log. The loop then moves on to 4 as normal.
// break: stops everything at 3
for (let i = 1; i <= 5; i++) {
if (i === 3) break;
console.log(i);
}
// prints 1, 2
// continue: skips only 3
for (let i = 1; i <= 5; i++) {
if (i === 3) continue;
console.log(i);
}
// prints 1, 2, 4, 5
| break | continue | |
|---|---|---|
| Current pass | Stops | Stops |
| Remaining passes | Cancelled | Still run |
| Works in switch? | Yes | No, loops only |
continue is handy for skipping items that do not qualify, keeping the main logic unindented:
const scores = [88, -1, 72, null, 95];
let total = 0;
let count = 0;
for (const s of scores) {
if (s === null || s < 0) continue; // skip bad data
total += s;
count++;
}
console.log("Average: " + (total / count).toFixed(1));
The same logic without continue would wrap the work inside an if. Both are correct; continue keeps the "skip" rule visible right at the top of the loop, like a bouncer at the door.
// Equivalent, using if instead of continue
for (const s of scores) {
if (s !== null && s >= 0) {
total += s;
count++;
}
}
for (let i = 1; i <= 10; i++) {
if (i % 2 === 0) continue; // even? skip it
console.log(i);
}
Be careful: in a while loop, continue jumps straight back to the condition. If the counter update comes after the continue, it gets skipped too:
// BUG: infinite loop when i hits 3
let i = 0;
while (i < 5) {
if (i === 3) continue; // jumps back before i++
console.log(i);
i++;
}
// FIX: update the counter first
let j = 0;
while (j < 5) {
j++;
if (j === 3) continue;
console.log(j);
}
for loop does not have this problem: its change step (i++) always runs after a continue. With while, you are responsible for making sure the counter still moves. Notice the fixed version also counts 1 to 5 instead of 0 to 4, because the counter now changes before the check.
Like break, continue can take a label (see JS Break for how labels work). A plain continue skips to the next pass of the innermost loop. continue labelname; skips to the next pass of the labeled loop instead:
let text = "";
loop1: for (let j = 1; j < 5; j++) {
loop2: for (let i = 1; i < 5; i++) {
if (i === 3) { continue loop2; } // skip just i = 3
text += i;
}
}
console.log(text);
let text = "";
loop1: for (let j = 1; j < 5; j++) {
loop2: for (let i = 1; i < 5; i++) {
if (i === 3) { continue loop1; } // abandon this inner run
text += i;
}
}
console.log(text);
With continue loop2, only i = 3 is skipped, and 4 still gets added: "124" four times. With continue loop1, hitting 3 abandons the rest of the inner loop and jumps to the outer loop's next pass, so 4 is never reached: "12" four times.
continue loop1 here gives exactly the same result as break loop2 from the Break lesson. "Skip to the outer loop's next pass" and "exit the inner loop" end up in the same place.
What does this log?
for (let i = 1; i <= 6; i++) {
if (i % 3 === 0) continue;
console.log(i);
}
1, 2, 4, 5. 3 and 6 are multiples of 3, so they are skipped. The loop still runs all 6 passes.Why does this loop never end?
let n = 0;
while (n < 10) {
if (n === 5) continue;
n++;
}
n reaches 5, continue jumps back to the condition before n++ runs. n stays 5 forever. Move n++ above the if, or use a for loop.You are adding up an array of prices and want to ignore any that are negative. Which fits best inside the loop?
if (price < 0) break;if (price < 0) continue;if (price >= 0) continue;while (price < 0) continue;continue to print 1 to 20, skipping every multiple of 3.length is its number of characters)if block instead of continuecontinue example with its Try it Yourself button and change continue loop2 to continue loop1. Then explain the buggy while example in your own words (don't run it: it freezes the tab)