Online Compiler logoOnline Compiler

JavaScript Tutorial

JavaScript continue Statement

Learn continue statements to skip loop iterations. Master flow control and filtering in loops.

Why this matters

continue statements improve code efficiency by skipping unnecessary iterations, making your loops more performant and your code more readable when filtering or validating data.

What is the continue Statement?

The continue statement skips the current iteration of a loop and continues with the next iteration.

It doesn't terminate the loop like break does - it just jumps to the next cycle.

Basic continue in for Loop

for (let i = 0; i < 10; i++) {
  if (i === 5) {
    continue; // Skip iteration when i is 5
  }
  console.log("Number:", i);
}

// Output:
// Number: 0
// Number: 1
// Number: 2
// Number: 3
// Number: 4
// Number: 6
// Number: 7
// Number: 8
// Number: 9

// Notice: 5 is missing!

When i equals 5, continue skips the console.log and moves to the next iteration.

continue in Different Loop Types

continue works with all loop types: for, while, do...while, and for...in/for...of loops.

continue in while Loop

let i = 0;

while (i < 10) {
  i++;

  if (i % 2 === 0) {
    continue; // Skip even numbers
  }

  console.log("Odd number:", i);
}

// Output:
// Odd number: 1
// Odd number: 3
// Odd number: 5
// Odd number: 7
// Odd number: 9

continue skips processing for even numbers but the loop continues.

continue in for...of Loop

const items = ['apple', 'banana', '', 'orange', '', 'grape'];

for (const item of items) {
  if (item === '') {
    continue; // Skip empty strings
  }

  console.log("Processing:", item);
}

// Output:
// Processing: apple
// Processing: banana
// Processing: orange
// Processing: grape

Empty strings are skipped, but the loop processes all non-empty items.

Common Use Cases

continue is useful for filtering, validation, and skipping unwanted iterations.

Filter Array Elements

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const evenNumbers = [];

for (const num of numbers) {
  if (num % 2 !== 0) {
    continue; // Skip odd numbers
  }
  evenNumbers.push(num);
}

console.log(evenNumbers); // [2, 4, 6, 8, 10]

Use continue to filter out unwanted elements during iteration.

Skip Invalid Data

const userData = [
  { name: 'Alice', age: 25 },
  { name: 'Bob', age: null },
  { name: 'Charlie', age: 30 },
  { name: '', age: 28 },
  { name: 'David', age: 35 }
];

for (const user of userData) {
  // Skip invalid entries
  if (!user.name || !user.age) {
    continue;
  }

  console.log(`Processing user: ${user.name}, age: ${user.age}`);
}

// Output:
// Processing user: Alice, age: 25
// Processing user: Charlie, age: 30
// Processing user: David, age: 35

continue helps skip invalid or incomplete data entries.

Process with Conditions

const files = ['document.txt', 'image.jpg', 'script.js', 'data.json'];

for (const file of files) {
  // Skip non-JavaScript files
  if (!file.endsWith('.js')) {
    continue;
  }

  console.log(`Compiling: ${file}`);
  // Compilation logic here...
}

// Output:
// Compiling: script.js

continue filters files by type before processing.

Skip Weekends in Date Processing

const dates = [
  new Date('2024-01-01'), // Monday
  new Date('2024-01-06'), // Saturday
  new Date('2024-01-08'), // Monday
  new Date('2024-01-13'), // Saturday
];

for (const date of dates) {
  const day = date.getDay(); // 0 = Sunday, 6 = Saturday

  if (day === 0 || day === 6) {
    continue; // Skip weekends
  }

  console.log(`Processing business day: ${date.toDateString()}`);
}

continue skips weekend dates in business logic processing.

continue vs break

continue skips one iteration, while break terminates the entire loop.

continue vs break Comparison

console.log("Using continue:");
for (let i = 0; i < 5; i++) {
  if (i === 2) {
    continue; // Skip this iteration
  }
  console.log(i); // 0, 1, 3, 4
}

console.log("\nUsing break:");
for (let i = 0; i < 5; i++) {
  if (i === 2) {
    break; // Exit entire loop
  }
  console.log(i); // 0, 1
}

continue skips iteration 2, break stops the loop completely at 2.

Performance Considerations

continue can improve performance by avoiding unnecessary processing.

Early Skip for Performance

const largeArray = Array.from({ length: 10000 }, (_, i) => i);

// Less efficient: process everything
let sum1 = 0;
for (const num of largeArray) {
  if (num % 2 === 0) { // Even check after loop overhead
    sum1 += num;
  }
}

// More efficient: skip early
let sum2 = 0;
for (const num of largeArray) {
  if (num % 2 !== 0) {
    continue; // Skip odd numbers immediately
  }
  sum2 += num;
}

console.log(sum1 === sum2); // true

continue can reduce unnecessary operations by skipping early.

Best Practices

Use continue to improve code readability and performance when skipping iterations.

  • Use continue for filtering and validation logic
  • Place continue early in loop body for better performance
  • Consider if break or return might be more appropriate
  • Avoid complex nested conditions before continue
  • Document why you're skipping iterations
  • Consider extracting skip logic into separate functions

Common Mistakes

  • Using continue in switch statements: continue only works in loops, not in switch statements. Use break in switches.
  • Placing continue too late: Put continue statements early to avoid unnecessary processing.
  • Overusing continue: If you have many continue statements, consider restructuring your loop logic.

Frequently Asked Questions

Does continue work in all types of loops?

Yes, continue works in for, while, do...while, for...in, and for...of loops.

What's the difference between continue and break?

continue skips the current iteration and continues with the next, while break exits the entire loop.

Can continue be used outside of loops?

No, continue can only be used inside loop statements.