continue

Skips the rest of the current loop turn and moves straight to the next one.

On this page

Example

#
JavaScript
const students = [
  { name: 'Emma', paid: true },
  { name: 'Jack', paid: false },
  { name: 'Mia', paid: true }
]

for (const student of students) {
  if (!student.paid) continue
  console.log(`Access for: ${student.name}`)
}
Console
Access for: Emma
Access for: Mia

Definition and usage

#

The continue statement ends only the current loop turn. The code below it is skipped, and the loop moves on to the next iteration. In for, the step, e.g. i++, still runs first, while in while and do...while the condition is checked right away.

It lets you reject elements that don’t match right at the top of the loop, instead of wrapping the rest of the code in a big if. It’s the same idea as an early return in a function.

Like break, it only works in loops, and with a label (continue outer) it jumps to the next turn of the outer loop. Inside a forEach() callback, its equivalent is return.

Syntax

#
Syntax
continue
continue label

More examples

#
Odd numbers in a for loop
JavaScript
const odd = []

for (let i = 1; i <= 10; i++) {
  if (i % 2 === 0) continue
  odd.push(i)
}

console.log(odd)
Console
[1, 3, 5, 7, 9]
continue in a while loop
JavaScript
let i = 0
const kept = []

while (i < 5) {
  i++
  if (i === 3) continue
  kept.push(i)
}

console.log(kept)
Console
[1, 2, 4, 5]

The counter is incremented before continue. If i++ were at the end of the loop, continue would skip past it and the loop would never finish.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • In while, change the counter before continue, otherwise the loop may never end.
  • If the whole loop only picks out elements, filter() says it more concisely: students.filter((student) => student.paid).

Related entries

#

Spotted a mistake or missing an example? Write to us.