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 withi = 1
and continues untili
reaches 5. - The
if
statement checks ifi
is even (i.e.,i % 2 === 0
). - If
i
is even, thecontinue
statement is triggered, which skips theconsole.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 withi = 0
and continues as long asi < 5
. - At the beginning of each iteration,
i
is incremented by 1 (i++
). - The
if
condition checks ifi
is even (i % 2 === 0
). If it is, thecontinue
statement is executed. - The
continue
statement skips theconsole.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 asi < 5
. - At the start of each iteration,
i
is incremented by 1 (i++
). - The
if
condition checks ifi
is even (i % 2 === 0
). If this is true, thecontinue
statement is triggered. - The
continue
statement skips theconsole.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 untili = 10
. - The
if
condition checks ifi
is divisible by 3 using the modulus operator (i % 3 === 0
). - If
i
is divisible by 3, thecontinue
statement is executed, skipping theconsole.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
usingouterLoop:
before the loop. - Inside the inner loop, when the condition
i === j
is true, thecontinue outerLoop;
statement is triggered. - This skips the rest of the current outer loop iteration (not just the inner loop).
- As a result, when
i
equalsj
, none of theconsole.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 afor
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!