In this section, we will learn what the continue statement is and how to use it in JavaScript.
What is continue Statement in JavaScript?
The JavaScript continue statement is used to stop the current iteration in a loop and move to its next iteration (if any).
Note that using this statement won’t skip the entire loop, but just the current iteration! So this means we’re still in the same loop and running its iteration, but just the current one will be skipped.
JavaScript continue Statement Syntax:
continue;
This statement does not take an argument.
Example: using continue statement in JavaScript for loop
const arr = []; for (let i = 0; i<10; i++){ if(i<5){ continue; } arr.push(i); } console.log(arr); console.log("Done!");
Output:
[5, 6, 7, 8, 9] Done!
In this example, as long as the value of the `i` variable in the loop is less than the value 5, the iteration will be skipped by calling the `continue;` statement and the next iteration will start. That’s why those numbers that were less than 5 weren’t sent to the output stream.
Example: using continue statement in JavaScript while loop
const array = ["Jack","Omid","Ian"] let num = -1; while (num++<array.length){ if (array[num] == "Omid"){ continue; } console.log(array[num]); }
Output:
Jack Ian
Example: JavaScript for of continue
const array = ["Jack","Omid","Ian"] for (const element of array){ if (element == "Omid"){ continue; } console.log(element); }
Output:
Jack Ian
More to Read:
JavaScritp break Statement TutorialÂ