In any walk, some items are none of the treatment's business: an invoice already settled, an empty row, an unsubscribed user. The usual reflex is to wrap the whole loop body in a condition.
continue offers the opposite: push the unwanted case aside in one line, at the top, and leave the main treatment flat.
Definition
continue drops the current loop turn and moves immediately to the next one. The walk is not interrupted, unlike what break does: only the lines below the keyword are skipped, and only for that item.
const invoices = [{ amount: 0 }, { amount: 120 }, { amount: 80 }];
let reminders = 0;
for (const invoice of invoices) {
if (invoice.amount === 0) continue; // nothing to chase on this one
reminders++;
}
console.log(reminders); // 2The gain is one level of indentation less, and a reading order that follows the reasoning: first what gets set aside, then what gets done. On a loop that sets three cases aside, the nested version asks for three more braces.
What separates it from break
| Question | break | continue |
|---|---|---|
| What stops | The whole walk | The current turn |
| Where execution resumes | After the loop | On the next turn |
| Usable inside a switch | Yes | No, only inside a loop |
The question to ask fits in one sentence: do the remaining items still hold any interest? If they do, it is continue; if they do not, it is break.
The trap of the counter that never moves
Inside a while loop, continue skips everything below it, including the line that advances the counter. The condition then stays true forever and the program freezes, with no error and no message.
let i = 0;
while (i < 3) {
if (i === 1) continue; // do not run this: i stops moving, the loop never ends
i++;
}Inside a do...while loop, continue does not jump back to the top of the block: it goes straight to the condition test. If that condition still holds, the body restarts from the beginning, exactly as after a full turn.
A for loop does not have that flaw: the update expression lives in the header and runs even when a continue has skipped the body.
for (let i = 0; i < 3; i++) {
if (i === 1) continue; // the header increment still happens
console.log(i); // 0 then 2
}Frequently asked questions
Is continue or filter the better choice?
When the exclusion rests on a single criterion and the treatment follows, Array.filter() states the intent more clearly: the list is narrowed first, walked second. continue keeps the edge when several exclusion conditions pile up, or when the exclusion has to be logged along the way.
Does continue work inside forEach?
The keyword itself is refused, since the body handed to Array.forEach() is a function and not a loop block. A return written in its place produces the same effect: it ends the call for the current item and the walk carries on. What stays impossible inside forEach is the equivalent of break.
Can you skip to the next turn of an outer loop?
Yes, by naming the outer loop and writing continue followed by that name. The current turn of the inner loop is dropped, and the outer loop is the one that advances. The construct is valid but rare: when it becomes necessary, extracting the inner loop into a function usually does the same job more readably.