Skip to main content

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

  • continue skips all remaining code in the iteration body and hands control to the next iteration.
  • break stops the loop entirely, continue stops 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 for loop the update step (i++) still runs after continue; in a while loop control goes straight to the condition check.
  • With a label, continue label jumps to the next iteration of the labelled outer loop.
  • continue does not work in forEach; there the same role is played by return.

Quick example

javascript
for (let i = 1; i <= 5; i++) { if (i === 3) continue; // skip 3 console.log(i); }

Result:

text
1 2 4 5

What 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

javascript
let i = 0; while (i < 5) { i++; if (i === 3) continue; console.log(i); }

Result:

text
1 2 4 5

As 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

KeywordWhat it does
breakStops the loop completely
continueSkips the rest of the loop body and moves to the next iteration

An example where both are used together:

javascript
for (let i = 1; i <= 5; i++) { if (i === 3) continue; // skip 3 if (i === 5) break; // stop the loop console.log(i); }

Result:

text
1 2 4

Filtering iterations by a condition

javascript
for (let i = 1; i <= 10; i++) { if (i % 2 === 0) continue; // skip even numbers console.log(i); }

Result:

text
1 3 5 7 9

This 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.

javascript
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:

text
i=1, j=1 i=2, j=1 i=3, j=1

Without 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

QuestionAnswer
What continue doesSkips the remaining code of the iteration and moves to the next one
Where it is usedOnly in loops (for, while, do...while, for...of, for...in)
Difference from breakbreak stops the loop completely
Working with labelscontinue label continues the labelled outer loop

Common mistakes

  • Placing continue in a while loop before the counter is incremented, which produces an infinite loop.
  • Expecting continue to work in forEach or map. Those are methods, not loops; there return plays the skipping role, and the iteration cannot be aborted at all.
  • Confusing continue with break: the first skips a step, the second ends the loop.
  • Using continue outside a loop, for example in a switch or just in a function body. That is a SyntaxError.
  • Overusing labels: two or three levels of labelled nesting read worse than an extracted function with a return.
  • Assuming continue also skips a trailing block such as try ... finally. The finally block runs in any case.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.