The continue keyword
The continue keyword is used inside loops and means "skip the rest of the current iteration and move on to the next one". The loop is not stopped completely, as it would be with break; it simply moves to the next step.
Theory
TL;DR
continueskips all remaining code in the iteration body and hands control to the next iteration.breakstops the loop entirely,continuestops only the current pass.- It works in every loop:
for,while,do...while,for...of,for...in. Outside a loop it is a syntax error. - In a
forloop the update step (i++) still runs aftercontinue; in awhileloop control goes straight to the condition check. - With a label,
continue labeljumps to the next iteration of the labelled outer loop. continuedoes not work inforEach; there the same role is played byreturn.
Quick example
for (let i = 1; i <= 5; i++) {
if (i === 3) continue; // skip 3
console.log(i);
}Result:
1
2
4
5What happened: at i = 3 the continue fired, JavaScript skipped console.log(i) and went straight to the next iteration (i = 4).
continue in a while loop
let i = 0;
while (i < 5) {
i++;
if (i === 3) continue;
console.log(i);
}Result:
1
2
4
5As soon as i === 3, continue skips console.log(i) and the loop goes back to checking i < 5. It is critical here that i++ comes before the continue: if the counter were incremented at the end of the body, continue would skip it and the loop would run forever. A for loop does not have this problem, because the i++ step lives in the loop header and always runs.
The difference between break and continue
| Keyword | What it does |
|---|---|
break | Stops the loop completely |
continue | Skips the rest of the loop body and moves to the next iteration |
An example where both are used together:
for (let i = 1; i <= 5; i++) {
if (i === 3) continue; // skip 3
if (i === 5) break; // stop the loop
console.log(i);
}Result:
1
2
4Filtering iterations by a condition
for (let i = 1; i <= 10; i++) {
if (i % 2 === 0) continue; // skip even numbers
console.log(i);
}Result:
1
3
5
7
9This is handy when an action should run only for certain elements. Such an early exit from the iteration keeps the main loop body free of extra if nesting, so the code reads better.
Labels and nested loops
continue can be used with a label when you need to move to the next iteration of an outer loop.
outer: for (let i = 1; i <= 3; i++) {
for (let j = 1; j <= 3; j++) {
if (j === 2) continue outer; // skip the rest of the inner loop
console.log(`i=${i}, j=${j}`);
}
}Result:
i=1, j=1
i=2, j=1
i=3, j=1Without a label, continue always refers to the nearest enclosing loop, so the only ways up a level are a label or extracting the inner loop into its own function.
Summary table
| Question | Answer |
|---|---|
What continue does | Skips the remaining code of the iteration and moves to the next one |
| Where it is used | Only in loops (for, while, do...while, for...of, for...in) |
Difference from break | break stops the loop completely |
| Working with labels | continue label continues the labelled outer loop |
Common mistakes
- Placing
continuein awhileloop before the counter is incremented, which produces an infinite loop. - Expecting
continueto work inforEachormap. Those are methods, not loops; therereturnplays the skipping role, and the iteration cannot be aborted at all. - Confusing
continuewithbreak: the first skips a step, the second ends the loop. - Using
continueoutside a loop, for example in aswitchor just in a function body. That is aSyntaxError. - Overusing labels: two or three levels of labelled nesting read worse than an extracted function with a
return. - Assuming
continuealso skips a trailing block such astry ... finally. Thefinallyblock runs in any case.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.