Skip For Loop Iteration | JavaScript


How to skip a for loop iteration in JavaScript?

You can use continue to skip a for loop iteration in JavaScript. Let’s see using an example. Here is the code to get sum of even numbers.

let count = 0;
for(let i = 0; i < 10; i++){
   if(i % 2 == 0) count+=i;
}
console.log('count is ', count);
// count is 20

Now let’s say you don’t want 4 to be included in sum.

let count = 0;
for(let i = 0; i < 10; i++){
   if(i == 4) continue;
   if(i % 2 == 0) count+=i;
}
console.log('count is ', count);
// count is 16

As seen above you are using continue to skip the loop iteration if 4 is encountered.