continue in JavaScript: moving to the next turn without leaving the loop

continue drops the current loop turn and moves straight on to the next item, without interrupting the walk or leaving the loop.
3 min read
Believemy logo

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.

JAVASCRIPT
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);   // 2

The 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

Questionbreakcontinue
What stopsThe whole walkThe current turn
Where execution resumesAfter the loopOn the next turn
Usable inside a switchYesNo, 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.

JAVASCRIPT
let i = 0;

while (i < 3) {
  if (i === 1) continue;   // do not run this: i stops moving, the loop never ends
  i++;
}
Good to know

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.

JAVASCRIPT
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

Question

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.


Question

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.


Question

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.

Related terms

Discover our javaScript glossary

Every word of JavaScript explained simply: keywords, built-in objects, methods, errors and concepts. Clear definitions and examples that actually run, to learn and to troubleshoot.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.