How to use the continue statement In Javascript

The continue statement in Javascript is used to break the iteration of the loop and follows with the next iteration. The break in the iteration is possible only when the specified condition going to occur.

Example: In this example, the continue statement is used to skip the even numbers and print only the odd numbers in the numbers array. The continue statement can be used to skip to the next iteration of a loop and abort the current iteration.

Javascript




let numbers = [1, 2, 3, 4, 5];
 
for (let i = 0; i < numbers.length; i++) {
    if (numbers[i] % 2 === 0) {
        continue;
    }
    console.log(numbers[i]);
}


Output

1
3
5

Different Ways to Abort JavaScript Execution

Aborting JavaScript execution can help us to write more robust, secure, and efficient code by preventing unexpected errors and behaviors.

Similar Reads

Possible reasons why we need to abort JavaScript execution are:

We might need to abort execution when an error occurs to prevent further execution of the code and avoid unexpected behavior or data corruption. We might need to abort execution when user input is invalid to prevent the code from running with incorrect or unexpected input. We might need to abort execution in the middle of a loop if a specific condition is met, or if we have found the result we were looking for. We might need to abort execution in some cases to optimize performance, such as when using setInterval or setTimeout, where we might need to stop the execution of a function or loop after a specific amount of time. In some cases, it might be necessary to abort execution to prevent malicious code from running, or to prevent sensitive data from being accessed....

Using the return statement

Return statements in a programming language are used to skip the currently executing function and return to the caller function....

Using the break statement

...

Using the continue statement

The break Statement comes out of the loop when the condition is true. It breaks out of the loop or switch....

Using the try-catch statement

...

Using the window.stop Method

The continue statement in Javascript is used to break the iteration of the loop and follows with the next iteration. The break in the iteration is possible only when the specified condition going to occur....

Using the clearTimeout() and clearInterval() Methods

...