Skip to main content

JavaScript Loops

JavaScript continue Statement

Written by Published

The JavaScript continue statement is used to skip one turn of a loop and move on to the next one.

When JavaScript finds a continue statement inside a loop, it stops the current turn and jumps straight to the next one.

The loop itself keeps running. Only the remaining code in that one turn is skipped.

Example

Example

javascript

for (let i = 1; i <= 5; i++) {
  if (i === 3) {
    continue;
  }

  console.log(i);
}

In this example, the number 3 is skipped.

The output is 1, 2, 4 and 5.

What is the continue Statement?

The continue statement is used to skip the rest of the current turn of a loop.

It is commonly used when you want to ignore some values but keep looping through the others.

This is the main difference from break. A break stops the loop completely, while continue only skips one turn.

Syntax

Syntax

javascript

continue;

The continue statement is usually written inside an if condition.

continue in a for Loop

You can use continue inside a for loop.

Example

Example

javascript

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

  console.log(i);
}

Here, every even number is skipped.

Only the odd numbers 1, 3, 5, 7 and 9 are printed.

continue in a while Loop

The continue statement can also be used inside a while loop.

Be careful to increase the counter before continue runs, or the loop will never end.

Example

Example

javascript

let i = 0;

while (i < 6) {
  i++;

  if (i === 4) {
    continue;
  }

  console.log(i);
}

Here, the number 4 is skipped.

The counter is increased before continue, so the loop still finishes.

continue in a do while Loop

You can also use continue inside a do while loop.

Example

Example

javascript

let i = 0;

do {
  i++;

  if (i === 2) {
    continue;
  }

  console.log(i);
} while (i < 5);

Here, the number 2 is skipped and the loop continues to 5.

Why Use continue?

The continue statement is useful when some values should be ignored but the loop should keep going.

Example

Example

javascript

let numbers = [10, -5, 20, -8, 30];

for (let i = 0; i < numbers.length; i++) {
  if (numbers[i] < 0) {
    continue;
  }

  console.log(numbers[i]);
}

In this example, the negative numbers are skipped.

Only 10, 20 and 30 are printed.

continue with Arrays

The continue statement is commonly used while filtering values in an array.

Example

Example

javascript

let fruits = ["Apple", "", "Mango", "", "Orange"];

for (let i = 0; i < fruits.length; i++) {
  if (fruits[i] === "") {
    continue;
  }

  console.log(fruits[i]);
}

The empty values are skipped and the fruit names are printed.

continue with Strings

You can also use continue while checking characters in a string.

Example

Example

javascript

let name = "Hello World";

for (let i = 0; i < name.length; i++) {
  if (name[i] === " ") {
    continue;
  }

  console.log(name[i]);
}

Here, the space character is skipped and every letter is printed.

continue vs break

It is easy to mix these two up, so it helps to see them side by side.

break leaves the loop. continue stays in the loop and moves to the next turn.

Example

Example

javascript

for (let i = 1; i <= 5; i++) {
  if (i === 3) {
    break;
  }

  console.log(i);
}

for (let i = 1; i <= 5; i++) {
  if (i === 3) {
    continue;
  }

  console.log(i);
}

The first loop prints 1 and 2, then stops completely.

The second loop prints 1, 2, 4 and 5, skipping only 3.

Complete Example

Complete Example

html

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript continue Statement</title>
</head>
<body>

  <h1>JavaScript continue Statement</h1>

  <p id="result"></p>

  <script>
    let result = "";

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

      result += "Odd number: " + i + "<br>";
    }

    document.getElementById("result").innerHTML = result;
  </script>

</body>
</html>

Try It Yourself

Run the above example in the Try It Editor.

Try changing this condition:

Replace i % 2 === 0 with i % 3 === 0 and see which numbers are skipped.

Important Points

  • The continue statement skips the rest of the current turn of a loop.
  • The loop does not stop; it moves on to the next turn.
  • It is commonly used with an if condition.
  • break exits the loop, while continue only skips one turn.
  • In a while loop, update the counter before continue or the loop will never end.

Conclusion

The JavaScript continue statement is used to skip a single turn of a loop.

It is useful when some values should be ignored but the rest still need to be processed.

Understanding continue helps you filter values inside loops and write cleaner JavaScript code.