The JavaScript continue statement
The continue statement skips the rest of the current loop iteration and jumps straight to the next one. if (skip) continue; is a clean way to ignore certain items mid-loop. Unlike break, it keeps the loop running; it only skips one pass.
Overview
continue says "skip the rest of this one, move on to the next." When it runs inside a loop, everything below it in the current iteration is skipped, and the loop proceeds to its next pass. It's the tidy way to handle exceptions in a loop (ignore the empty rows, skip the disabled items, pass over anything that doesn't qualify) without wrapping the whole body in an if.
The contrast with break is the key thing to keep straight: break leaves the loop entirely, while continue stays in the loop and just skips the current iteration. One stops; the other steps over.
Used well, an early continue (a "guard") flattens loop bodies and improves readability: if (!item.active) continue; at the top reads cleanly. That said, when you're continuing in order to process only a subset, a filter() (often chained with map()) frequently expresses the intent more declaratively than a loop with continue.
Syntax
for (const item of items) {
if (!item.active) {
continue; // skip inactive items
}
process(item);
}
Example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
const nums = [1, 2, 3, 4, 5, 6];
const odds = [];
for (const n of nums) {
if (n % 2 === 0) continue; // skip evens
odds.push(n);
}
document.getElementById('out').textContent = 'odds: ' + odds.join(', '); // 1, 3, 5
</script>
Best practices
Frequently asked questions
What does continue do in JavaScript?
What is the difference between continue and break?
continue skips just the current iteration; break exits the loop completely.When should I use continue instead of an if?
continue guard (if (skip) continue;) keeps the main loop body un-nested, which is often more readable than wrapping everything in an if.