JavaScript continue statement

JavaScript: continue Statement

In JavaScript, the continue statement is a powerful tool used within loops. Its primary function is to skip the current iteration of a loop and immediately move on to the next one. When the continue statement is encountered, the remaining code inside the loop is ignored for that iteration, and the loop proceeds to the next cycle.

This statement is especially useful when you want to skip certain conditions but still continue looping through the rest of the elements. By using continue, you can streamline your loop’s flow and avoid unnecessary computations or actions.

In this article, we’ll explore how to use the continue statement effectively across different loop structures in JavaScript, including for, while, and do-while loops, as well as how to incorporate it in more complex scenarios.

Basic Syntax of the continue Statement

The syntax for the continue statement is very simple:

continue;

When the continue statement is encountered inside a loop, it immediately skips the remaining code within that loop iteration and proceeds to the next iteration. This means that any code written after the continue in that particular iteration will not be executed, and the loop will jump to the next cycle.

It’s important to note that continue only works within loops such as for, while, and do-while. If used outside of a loop, it will result in an error.

continue in a for Loop

The continue statement is often used in a for loop when you want to skip the current iteration based on a certain condition.

for (let i = 1; i <= 5; i++) {

  if (i % 2 === 0) continue;
  console.log(i);

}
// Output: 1, 3, 5

In this example, the continue statement is used to skip even numbers. Here’s how it works:

  • The for loop starts with i = 1 and continues until i reaches 5.
  • The if statement checks if i is even (i.e., i % 2 === 0).
  • If i is even, the continue statement is triggered, which skips the console.log(i) line and moves to the next iteration of the loop.
  • As a result, the numbers 2 and 4 are skipped, and the output is 1, 3, 5.

The continue statement allows you to bypass certain code in a loop, making the loop more flexible and efficient when dealing with specific conditions.

continue in a while Loop

The continue statement can also be used in a while loop to skip the rest of the code and proceed to the next iteration when a condition is met.

let i = 0;

while (i < 5) {

  i++;

  if (i % 2 === 0) continue;

  console.log(i);

}
// Output: 1, 3, 5

In this example, the continue statement is used inside a while loop to skip even numbers. Here’s how it works:

  • The while loop starts with i = 0 and continues as long as i < 5.
  • At the beginning of each iteration, i is incremented by 1 (i++).
  • The if condition checks if i is even (i % 2 === 0). If it is, the continue statement is executed.
  • The continue statement skips the console.log(i) statement and jumps to the next iteration of the loop.
  • As a result, the even numbers 2 and 4 are skipped, and the output is 1, 3, 5.

This shows that in both for and while loops, the continue statement works similarly by skipping the remaining code in the current iteration and moving to the next one.

continue in a do-while Loop

The continue statement can also be used in a do-while loop to skip the rest of the code for the current iteration and move to the next iteration.

let i = 0;

do {

  i++;

  if (i % 2 === 0) continue;

  console.log(i);

} while (i < 5);
// Output: 1, 3, 5

In this example, the continue statement is used inside a do-while loop to skip over even numbers:

  • The loop begins with i = 0 and continues to run as long as i < 5.
  • At the start of each iteration, i is incremented by 1 (i++).
  • The if condition checks if i is even (i % 2 === 0). If this is true, the continue statement is triggered.
  • The continue statement skips the console.log(i) and jumps to the next iteration of the loop.
  • As a result, even numbers like 2 and 4 are skipped, and the output is 1, 3, 5.

In a do-while loop, just like in for and while loops, the continue statement causes the loop to jump directly to the next iteration, skipping the remaining code in the current one. The key difference in a do-while loop is that it will always execute the code block at least once, even if the condition is false from the start.

Skipping Iterations Based on Conditions

The continue statement is often used in combination with conditional statements to control when an iteration should be skipped. This allows for more granular control over the flow of loops.

for (let i = 1; i <= 10; i++) {

  if (i % 3 === 0) continue;  // Skip numbers divisible by 3
  console.log(i);

}
// Output: 1, 2, 4, 5, 7, 8, 10

In this example, the continue statement is used to skip numbers that are divisible by 3:

  • The loop starts with i = 1 and runs until i = 10.
  • The if condition checks if i is divisible by 3 using the modulus operator (i % 3 === 0).
  • If i is divisible by 3, the continue statement is executed, skipping the console.log(i) statement for that iteration.
  • The loop then moves to the next value of i without printing the skipped value.
  • As a result, numbers like 3, 6, and 9 (which are divisible by 3) are not printed, and the output is 1, 2, 4, 5, 7, 8, 10.

This approach using continue allows you to easily control the flow of your loop by skipping over certain iterations based on specific conditions.

Using continue with Nested Loops

When working with nested loops, the continue statement affects only the innermost loop. This means that the outer loop will continue its execution, but the inner loop will skip the current iteration and move to the next.

for (let i = 0; i < 3; i++) {

  for (let j = 0; j < 3; j++) {

    if (i === j) continue;  // Skip when i equals j

    console.log(`i: ${i}, j: ${j}`);

  }

}
// Output:
// i: 0, j: 1
// i: 0, j: 2
// i: 1, j: 0
// i: 1, j: 2
// i: 2, j: 0
// i: 2, j: 1

In this case, the continue only affects the inner loop (the loop that contains the continue), so the outer loop (i) continues its iteration while the inner loop (j) skips the corresponding iteration when the condition is met. This behavior is essential when you need fine control over skipping iterations in nested loops.

continue with Labels (Optional)

Sometimes, when you’re working with nested loops, you might want to skip the current iteration of an outer loop instead of just the inner one. JavaScript allows this using labels with the continue statement.

outerLoop: for (let i = 0; i < 3; i++) {

  for (let j = 0; j < 3; j++) {

    if (i === j) continue outerLoop;  // Skip the outer loop iteration
    console.log(`i: ${i}, j: ${j}`);

  }

}
// Output:
// i: 1, j: 0
// i: 2, j: 0
// i: 2, j: 1

In this example:

  • The outer loop is labeled outerLoop using outerLoop: before the loop.
  • Inside the inner loop, when the condition i === j is true, the continue outerLoop; statement is triggered.
  • This skips the rest of the current outer loop iteration (not just the inner loop).
  • As a result, when i equals j, none of the console.log statements run for that outer loop cycle.

This labeled approach gives you more control when working with complex loop structures. But it’s optional and should be used carefully to keep code easy to read.

Practical Example: Filtering Data

The continue statement is handy when you want to skip over certain values while looping—like filtering out bad or unwanted data.

const data = [10, 20, -5, 30, 0, -1, 40];

for (let i = 0; i < data.length; i++) {

  if (data[i] < 0) continue;  // Skip negative numbers
  console.log(data[i]);

}
// Output:
// 10
// 20
// 30
// 0
// 40

In this example:

  • We loop through the data array using a for loop.
  • Inside the loop, we check if the number is negative (data[i] < 0).
  • If it is, continue skips the rest of the code and jumps to the next item in the loop.
  • This way, only non-negative numbers (zero and positive values) get printed.

This shows how continue is useful for filtering values without needing extra if-else blocks, keeping the code clean and easy to read.

Conclusion

The continue statement is a simple but powerful tool in JavaScript. It lets you skip parts of a loop when certain conditions are met, making your code cleaner and easier to follow. Whether you’re using a for, while, or do-while loop—or even working with nested loops—continue helps you avoid extra if-else logic and focus only on the data or steps that matter.

We saw how to use continue in basic loops, with conditions, in nested structures, and even for practical tasks like filtering data. Now that you know how it works, try using it in your own projects to control loops in smarter ways. The more you practice, the better you’ll get at writing clean and efficient code!

Scroll to Top