JavaScript continue Statement


In JavaScript, sometimes you may want to skip the rest of the current iteration of a loop and proceed directly to the next iteration. This is where the continue statement comes in handy. It allows you to bypass the remaining code inside the loop for the current iteration and move to the next one.


1. What is the continue Statement in JavaScript?

The continue statement is used inside loops to skip the current iteration and move to the next one. When the continue statement is encountered, it immediately ends the current iteration and proceeds with the next cycle of the loop.

The continue statement can be used in all loop structures such as for, while, and do...while.

Syntax of the continue Statement:

continue;
  • When executed inside a loop, it skips the remaining code in the current iteration and proceeds to the next iteration.

2. Using continue in a for Loop

The continue statement is often used in a for loop when you need to skip specific iterations based on a condition.

Example 1: Using continue in a for Loop

for (let i = 0; i < 10; i++) {
  if (i % 2 === 0) {
    continue;  // Skip the rest of the code when i is even
  }
  console.log(i);  // This code runs only when i is odd
}

Explanation:

  • The loop runs from i = 0 to i = 9.
  • When i is even, the continue statement skips the remaining code inside the loop, so console.log(i) is not executed.
  • The output will be:
    1
    3
    5
    7
    9
    

Here, the continue statement ensures that only odd numbers are printed.


3. Using continue in a while Loop

You can also use the continue statement in a while loop to skip specific iterations and continue to the next iteration of the loop.

Example 2: Using continue in a while Loop

let i = 0;

while (i < 10) {
  i++;
  if (i % 2 === 0) {
    continue;  // Skip the rest of the loop when i is even
  }
  console.log(i);  // This code runs only when i is odd
}

Explanation:

  • The while loop runs as long as i is less than 10.
  • The continue statement skips the console.log(i) statement when i is even.
  • The output will be:
    1
    3
    5
    7
    9
    

As in the previous example, only the odd numbers are printed because the continue statement skips even numbers.


4. Using continue in a do...while Loop

The continue statement can also be used in a do...while loop to skip the rest of the iteration and proceed to the next iteration of the loop.

Example 3: Using continue in a do...while Loop

let i = 0;

do {
  i++;
  if (i % 2 === 0) {
    continue;  // Skip the rest of the loop when i is even
  }
  console.log(i);  // This code runs only when i is odd
} while (i < 10);

Explanation:

  • The do...while loop ensures that the loop runs at least once before checking the condition (i < 10).
  • The continue statement skips the console.log(i) statement when i is even.
  • The output will be:
    1
    3
    5
    7
    9
    

As in the other loop examples, only odd numbers are printed because the continue statement skips even numbers.


5. Practical Use Cases of continue

Example 4: Skipping Invalid Entries in User Input

Imagine you are processing user input and want to skip invalid entries, such as empty or incorrect values. The continue statement is a great way to skip invalid inputs without breaking the entire loop.

let inputs = ["", "hello", "123", "world", " ", "JavaScript"];
for (let i = 0; i < inputs.length; i++) {
  if (inputs[i].trim() === "") {
    continue;  // Skip empty or whitespace strings
  }
  console.log("Valid input: " + inputs[i]);
}

Explanation:

  • The continue statement skips over the empty or whitespace-only strings in the inputs array.
  • The valid inputs (non-empty strings) are printed out.
  • The output will be:
    Valid input: hello
    Valid input: 123
    Valid input: world
    Valid input: JavaScript
    

In this example, the continue statement helps filter out invalid entries and ensures that only valid inputs are processed.


6. continue with Labels

Just like the break statement, you can use labeled continue to skip the current iteration of an outer loop from within a nested loop.

Example 5: Using Labeled continue to Skip an Outer Loop

outerLoop:
for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 3; j++) {
    if (i === 1 && j === 1) {
      continue outerLoop;  // Skip to the next iteration of the outer loop
    }
    console.log("i = " + i + ", j = " + j);
  }
}

Explanation:

  • The continue outerLoop statement skips the inner loop entirely when i === 1 and j === 1, and moves to the next iteration of the outer loop.
  • The output will be:
    i = 0, j = 0
    i = 0, j = 1
    i = 0, j = 2
    i = 1, j = 0
    i = 1, j = 2
    i = 2, j = 0
    i = 2, j = 1
    i = 2, j = 2
    

The continue outerLoop statement helps control the flow of the outer loop from within the inner loop