Course Content
Introduction to JavaScript
Introduction to JavaScript
continue
The continue
keyword allows you to skip the remaining code within a loop for the current iteration and proceed to the next iteration.
Example 1: Skipping Early Iterations
for (let i = 0; i < 10; i++) { if (i < 5) { continue; } console.log("i =", i); }
In this example, the continue
keyword is used to skip iterations of the loop where i
is less than 5
. As a result, only iterations with i
values in the range [5, 9] execute the code inside the loop.
Example 2: Skipping a Specific Iteration
for (let i = 1; i <= 5; i++) { console.log("Iteration started:", i); if (i == 3) { continue; // Skip the end of the 3rd iteration } console.log("- Iteration ended:", i); }
Here, the continue
statement is used to skip the end of the 3rd iteration, resulting in the output of "Iteration started" and "Iteration ended" messages for all iterations except the one where i
equals 3.
Note
The
continue
keyword works similarly tobreak
, but instead of terminating the loop entirely, it only skips the current iteration and proceeds with the next one. This behavior can be handy for fine-grained control of loop execution.
Thanks for your feedback!