By the end of this lesson, you will be able to use JavaScript continue to skip the rest of the current loop iteration and move to the next one.
What it is
continue is a control-flow statement used inside loops such as for, while, do...while, for...in, and for...of. When the JavaScript engine reaches continue, it stops executing the remaining statements in the current iteration and starts the next iteration.
A useful mental model is a playlist: continue is like skipping the current song and moving to the next one. It does not stop the playlist; break does that. Related terms include iteration, loop body, labeled statement, and control flow.
Why it matters
- It lets you filter items without wrapping the whole loop body in a large
ifblock. - It can skip invalid or unwanted data early, such as empty strings, negative numbers, or missing fields.
- It reduces nesting, making loops easier to read.
- With a label, it can skip to the next iteration of an outer loop in nested loops.
Syntax or steps
The smallest useful pattern is:
if (skipCondition) {
continue;
}
Place this inside a loop. If skipCondition is truthy, the rest of the loop body is skipped. If it is falsy, execution continues normally.
For nested loops, you can label an outer loop and continue it:
outer: for (const row of matrix) {
for (const value of row) {
if (value === 0) {
continue outer;
}
}
}
This skips the rest of the current outer iteration when a zero is found.
Example
const nums = [1, 2, 3, 4, 5, 6];
let evenSum = 0;
for (const n of nums) {
if (n % 2 !== 0) {
continue;
}
evenSum += n;
}
console.log(evenSum); // 12
Part by part:
const nums = ...creates an array of numbers.let evenSum = 0stores the running total of even numbers.for (const n of nums)visits each number one at a time.if (n % 2 !== 0)checks whether the number is odd.continueskips the addition when the number is odd.evenSum += nruns only for even numbers.console.log(evenSum)prints12, because2 + 4 + 6equals12.
Common mistakes
- Using continue outside a loop. This causes a
SyntaxError. Fix it by placingcontinueinside a loop body. - Thinking continue exits the loop. It only skips the current iteration. Use
breakwhen you want to stop the loop entirely. - Forgetting to update the loop variable in a while loop. If
continueruns before an increment, the loop may never advance. Fix it by incrementing beforecontinueor using aforloop. - Using continue inside a switch and expecting it to exit the switch. In JavaScript,
continueaffects the enclosing loop, not the switch. Usebreakto exit a switch case.
When to use it
Use continue when some items should be ignored but the loop should keep running. Use break when the loop should stop. Use return when you are inside a function and want to exit the function.
| Statement | Effect | Best when |
|---|---|---|
continue | Skips the rest of the current iteration | Filtering unwanted items |
break | Exits the loop | Found what you need or cannot continue |
return | Exits the function | Inside a function and done with the result |
Practice
Guided exercise: Loop over const scores = [42, 78, 55, 31, 90]. Skip scores below 50 and log the passing scores.
const scores = [42, 78, 55, 31, 90];
for (const score of scores) {
if (score < 50) {
continue;
}
console.log(score);
}
Expected output:
78
55
90
Challenge: Given const names = [" Ada ", "", " Grace ", " "], skip empty or whitespace-only names and log the trimmed names. Hint: use name.trim() and check its length.
Quick check
Question: In a for loop, what happens after continue runs?
Answer: The rest of the loop body is skipped, the loop’s update expression runs, and the condition is checked for the next iteration.
Summary
continue is a simple way to skip unwanted loop iterations while keeping the loop alive. It is especially useful for filtering data and reducing nested if blocks. Use it when you want to move on, not stop.