JavaScript Continue Statement: Skipping Iterations
The continue statement skips the rest of the current loop iteration and jumps to the next one. Learn how to cleanly filter out items without deeply nested if blocks.
The continue statement tells JavaScript to skip the rest of the current loop iteration and jump straight to the next one. The loop does not stop -- it just ignores whatever comes after continue in that particular pass and moves on.
It works in for, while, and do while loops. It does not work in forEach or other array methods.
Basic continue in a for Loop
The simplest use of continue is filtering out values you do not want to process:
for (let i = 0; i < 6; i++) {
if (i % 2 !== 0) {
continue;
}
console.log(i);
}Each odd value triggers continue and skips straight past the print statement, while every even value falls through and gets logged. Output:
0
2
4When i is odd, continue runs and the print statement is skipped. The loop still increments i via the update expression, checks the condition, and continues to the next even number. Only odd values are filtered out.
In a for loop, continue does NOT skip the update expression. The counter always increments, which prevents the loop from getting stuck on the same iteration. This is different from while loops, where you must be more careful.
continue in a while Loop
In a while loop, continue jumps to the condition check. You must make sure the variable that controls the condition is updated before the continue:
let i = 0;
while (i < 5) {
i++;
if (i === 3) {
continue;
}
console.log(i);
}Because i increments before the check, continue only ever skips the print for the value 3, and the loop keeps advancing normally afterward. Output:
1
2
4
5Notice the increment comes before the continue check. If it were after, the loop would get stuck at i equal to 3 forever because i would never increment past 3. In a for loop the update runs automatically; in a while loop you manage it yourself.
Using continue as a Guard Clause
Continue lets you write flat, readable loop bodies by bailing out early for uninteresting cases:
const students = [
{ name: "Alice", score: 92 },
{ name: "Bob", score: 45 },
{ name: "Carol", score: 78 },
];
for (const student of students) {
if (student.score < 50) continue;
if (student.name === "Bob") continue;
console.log(`${student.name}: pass`);
}Each continue acts as an early exit for one filter condition, so only students who clear both checks reach the final print statement. Output:
Alice: pass
Carol: passWithout continue, the exact same filtering logic would require nested if blocks instead of flat, early-exit checks, and it gets harder to read as more conditions are added:
for (const student of students) {
if (student.score >= 50) {
if (student.name !== "Bob") {
console.log(`${student.name}: pass`);
}
}
}The continue version is easier to scan because each filter condition stands on its own line. You read down the list: skip low scores, skip Bob, process the rest.
Labeled continue for Nested Loops
A plain continue only affects the innermost loop. To skip to the next iteration of an outer loop from inside an inner loop, use a label:
outer: for (let i = 1; i <= 3; i++) {
for (let j = 1; j <= 3; j++) {
if (j === 2) {
continue outer;
}
console.log(`i=${i}, j=${j}`);
}
}Because the label targets the outer loop, continue outer abandons the rest of the inner loop entirely and jumps straight to the outer loop's next value. Output:
i=1, j=1
i=2, j=1
i=3, j=1When j equals 2, continue outer skips the rest of the inner loop AND skips to the next outer iteration. The inner loop never reaches j equal to 3 for any outer iteration, because j reaching 2 always triggers the skip to the next i.
continue vs break vs return
| Statement | Effect | Loop Continues? |
|---|---|---|
continue | Skip current iteration | Yes, next iteration runs |
break | Exit entire loop | No, loop stops |
return | Exit entire function | No, function stops |
function processNumbers(nums) {
for (const n of nums) {
if (n === 0) continue; // Skip zeros
if (n < 0) break; // Stop at first negative
console.log(n);
}
console.log("Done with loop");
}
processNumbers([5, 0, 8, -3, 2]);
// Prints: 5, 8, Done with loopZero is skipped with continue. The negative number triggers break, stopping the loop. The function still prints "Done with loop" because break only exits the loop, not the function.
Common Mistakes
Putting continue after the code you want to run. continue must come before the logic you want to skip:
// WRONG: the console.log always runs
for (let i = 0; i < 3; i++) {
console.log(i);
if (i === 1) continue; // Too late, already printed
}
// RIGHT: continue first
for (let i = 0; i < 3; i++) {
if (i === 1) continue;
console.log(i);
}Using continue in forEach. Like break, continue is a syntax error inside forEach. Use return to skip in forEach, or switch to a for...of loop:
// forEach: use return to skip
[1, 2, 3].forEach((n) => {
if (n === 2) return; // Skips this callback, next item still runs
console.log(n);
});
// Prints: 1, 3Forgetting to update the counter before continue in a while loop. If the counter update is after continue, the loop becomes infinite:
// DANGEROUS: infinite loop when i hits 3
let i = 0;
while (i < 5) {
if (i === 3) continue; // Jumps back to condition, i stays 3
console.log(i);
i++;
}Always place the counter update before any continue in a while or do while loop. In a for loop, the update runs automatically in the header.
For the counterpart that stops the loop entirely, see the break statement guide. For the subtle bug this pattern can cause, see the guide on avoiding infinite loops.
Rune AI
Key Insights
- continue skips the rest of the current iteration and starts the next one.
- In a for loop, continue still runs the update expression before the next check.
- Use continue as an early guard to keep the main loop body unindented.
- Labeled continue lets you skip to the next iteration of an outer loop.
- continue does not work in forEach; use return instead.
Frequently Asked Questions
Does continue work in forEach?
What is the difference between continue and break?
Does continue skip the update step in a for loop?
Conclusion
The continue statement keeps your loop logic flat and readable. Instead of wrapping the entire loop body in an if block, use continue to bail out of uninteresting iterations early. Combined with labeled continue for nested loops, it gives you precise control over which iterations to skip.
More in this topic
JavaScript While Loop Explained: A Complete Guide
A while loop repeats code as long as a condition stays true. Learn the syntax, see practical examples, and understand when a while loop is the right choice over a for loop.
JavaScript Loops Tutorial: for, while, do while
Loops let JavaScript repeat code without writing it twice. Learn the three core loop types, what each one does, and when to choose one over another.
How to Loop Through Arrays Using JS for Loops Guide
Looping through arrays is the most practical use of JavaScript for loops. Learn how to access each element by index, avoid off-by-one errors, and choose between for, for...of, and forEach.