JavaScript > Break and Continue

Continue loop in JavaScript

How to skip the current iteration of the loop and continue with the next iteration in JavaScript?


The continue statement is used to skip the current iteration and continue with the next iteration of the loop.

    <script>
        function TestFunction() {
            var i = 0;
            do {
                i++;
                if (i == 2) {
                    continue;
                }
                alert(i);
            } while (i < 5);
        }

    </script>

    <input type="button" id="btnTest" onclick="TestFunction()" value="Click me" />

Above loop executes 4 times only and gives alert as 1, 3, 4, 5. It doesn’t give 2 as alert because in 2nd iteration it goes into the if block and due to continue statement it skips that iteration and jump to the next iteration of the loop. That is shown clearly in the Demo.

 Views: 4292 | Post Order: 30



Write for us






Hosting Recommendations