0
0
Javascriptprogramming~5 mins

Continue statement in Javascript

Choose your learning style9 modes available
Introduction

The continue statement helps skip the current step in a loop and move to the next one. It lets you ignore some steps without stopping the whole loop.

When you want to skip processing certain items in a list but keep looping through the rest.
When checking conditions inside a loop and ignoring some cases without breaking the loop.
When filtering data on the fly and only acting on specific values.
When you want to avoid nested if-else blocks by skipping unwanted cases early.
Syntax
Javascript
continue;

The continue statement is used inside loops like for, while, or do...while.

When JavaScript sees continue, it skips the rest of the current loop step and moves to the next iteration.

Examples
This loop skips printing the number 2 but prints all other numbers from 0 to 4.
Javascript
for (let i = 0; i < 5; i++) {
  if (i === 2) continue;
  console.log(i);
}
This while loop skips printing 3 but prints other numbers from 1 to 5.
Javascript
let i = 0;
while (i < 5) {
  i++;
  if (i === 3) continue;
  console.log(i);
}
Sample Program

This program prints only odd numbers from 1 to 10 by skipping even numbers using continue.

Javascript
for (let num = 1; num <= 10; num++) {
  if (num % 2 === 0) continue;
  console.log(num);
}
OutputSuccess
Important Notes

Using continue can make your loops cleaner by avoiding deep nesting of conditions.

Be careful not to create infinite loops by skipping the part where the loop variable changes.

Summary

continue skips the rest of the current loop step and moves to the next one.

It is useful to ignore certain cases inside loops without stopping the whole loop.

Works inside for, while, and do...while loops.