Online Compiler logoOnline Compiler

JavaScript Tutorial

JavaScript continue Statement

The continue statement skips the current iteration and jumps to the next one.

It helps filter items without deep nesting.

Why We Need It

Filtering values is common when processing arrays or streams.

continue keeps the loop body clean and focused on valid items.

Syntax

continue;

Basic Example

1. Skip an item

for (let i = 0; i < 5; i++) {
  if (i === 2) continue;
  console.log(i);
}

The value 2 is skipped.

Real World Example

2. Filter odd numbers

const nums = [1, 2, 3, 4, 5];

for (const n of nums) {
  if (n % 2 !== 0) continue;
  console.log(n);
}

continue skips all odd numbers.

Multiple Use Cases

Skip an Iteration

continue skips the rest of the current loop iteration and moves to the next one.

It is useful for filtering values without deeply nested conditions.

Filtering Patterns

continue makes it easy to ignore certain items while looping.

It keeps the main logic flatter and easier to read.

Combine with Guards

Use continue alongside guard checks to avoid nested if blocks.

Be careful to still update counters to avoid infinite loops.

More Examples

3. Guard in while

let i = 0;

while (i < 5) {
  i++;
  if (i === 3) continue;
  console.log(i);
}

Remember to update the counter before continuing.

4. Skip empty values

const items = ["a", "", "b"];

for (const item of items) {
  if (!item) continue;
  console.log(item);
}

Skip empty strings with a continue guard.

Comparison

Without

for (const n of nums) {
  if (n % 2 === 0) {
    console.log(n);
  }
}

With

for (const n of nums) {
  if (n % 2 !== 0) continue;
  console.log(n);
}

Common Mistakes and Fixes

Forgetting to update counters

Make sure the loop state still changes before continue.

Overusing continue

Use clear conditions; too many continues can be confusing.

Skipping needed work

Ensure skipped iterations do not miss required side effects.

Interview Questions

What does continue do?

It skips the rest of the current loop iteration.

When is continue useful?

When filtering items without nested if blocks.

How do you avoid infinite loops?

Update counters before continue in while loops.

Practice Problem

Practice: Print only the positive numbers from an array using continue.

const nums = [-1, 2, 0, 3, -5];
// TODO: print only positives

One Possible Solution

const nums = [-1, 2, 0, 3, -5];
for (const n of nums) {
  if (n <= 0) continue;
  console.log(n);
}

Frequently Asked Questions

What does continue do?

It skips the rest of the current iteration and moves to the next one.

How is continue different from break?

Break exits the loop entirely; continue only skips one iteration.

Can I use continue in while loops?

Yes, but make sure the loop state still changes.

Try It Yourself

Try skipping different values and see how the output changes.