How to use Break and Continue Statement in JavaScript

In this article I am going to explain about Break and Continue statement in JavaScript
  • 2277

Break and Continue Statement

Break statement use for break the loop. After breaking loop code execution will  be continue after loop.

continue statement is also use for break the loop but its not break all loop. Its break only current loop and loop will be continue with next value.

How to use Break

In this example when "i"value will be 8 then loop will break.

<html>

<head>

    <script type="text/javascript">

 

                      function MyFunction() {

            for (i = 0; i < 15; i++) {

                if (i == 8) {

                    break;

                }

                document.write(i+",");

            }

        }

       

    </script>

</head>

<body>

    <button onclick="MyFunction()">

        Click</button>

</body>

</html>

 

Output


break.jpg

 

How to use Continue

 

In this example when "i" value will be 8 then current loop will be break but loop loop will be continue with next value.

 

<html>

<head>

    <script type="text/javascript">

 

                      function MyFunction() {

            for (i = 0; i <= 15; i++) {

                if (i == 8) {

                    continue;

                }

                document.write(i+",");

            }

        }

       

    </script>

</head>

<body>

    <button onclick="MyFunction()">

        Click</button>

</body>

</html>

 

Output


continue.jpg


Further Readings

You may also want to read these related articles: here
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.