
JavaScript continue statement
The JavaScript continue
statement is used within loops (such as for
, while
, or do-while
) to skip the rest of the current iteration and move to the next iteration. It is often used when you want to skip a specific iteration or certain condition and continue the loop execution.
Here’s an example that demonstrates the continue
statement in a for
loop:
for (let i = 0; i < 5; i++) {
if (i === 2) {
continue; // Skip iteration when i is 2
}
console.log(i);
}
In this example, the loop will iterate from i = 0
to i = 4
. When i
is equal to 2, the continue
statement is encountered, and it skips the rest of the code within the current iteration. As a result, the number 2 is not printed to the console. The loop continues to the next iteration, and the remaining numbers are logged.
The continue
statement can also be used in other loop types, such as while
or do-while
, in a similar manner. It helps control the flow of the loop and allows you to skip certain iterations based on specific conditions.
It’s worth noting that the continue
statement only affects the innermost loop it is placed in. If you have nested loops, the continue
statement will only skip the current iteration of the inner loop and continue with the next iteration of that loop.