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

That is, the loop does not stop completely (as with break), it simply moves on to the next step.


Example with for

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

Result:

javascript
1 2 4 5

What happened:

  • At i = 3, continue fired
  • JS skipped console.log(i) and jumped straight to the next iteration (i = 4).

Example with while

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

Result:

javascript
1 2 4 5

As soon as i === 3, the continue statement skips console.log(i), and the loop returns to checking the condition i < 5.


The difference between break and continue

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

Example:

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:

javascript
1 2 4

Example with a condition

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

Result:

javascript
1 3 5 7 9

Convenient when you need to act only on certain elements.


Example with nested loops

continue can be used with a label, if you need to move on to the next iteration of the 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 remaining steps of the inner loop console.log(`i=${i}, j=${j}`); } }

Result:

javascript
i=1, j=1 i=2, j=1 i=3, j=1

Summary

KeywordBehavior
continueSkips 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

In short

  • continue means "move on to the next iteration".
  • It skips the remaining code in the current loop.
  • It is convenient when you need to "filter" iterations by a condition, without stopping the loop completely.

Short Answer

Interview ready
Premium

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