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
5What happened:
- At
i = 3,continuefired - 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
5As soon as
i === 3, thecontinuestatement skipsconsole.log(i), and the loop returns to checking the conditioni < 5.
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 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
4Example 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
9Convenient 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=1Summary
| Keyword | Behavior |
|---|---|
continue | 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 |
In short
continuemeans "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 readyPremium
A concise answer to help you respond confidently on this topic during an interview.