How to use While loop in JavaScript

In this article I am going to explain about while loop in JavaScript.
  • 2449

JavaScript While loop

We use loop when we want to execute any code over and over again till specified number of time. While loop is an also type of loop it will execute code until given condition is true.

While loop syntax

while (Variable> condition)

        {

            //Our code that should be run

        }

In this example it will display 1 to 10 natural number through while loop

<html>

<head>

    <script type="text/javascript">

 

                      function MyFunction() {

            while (i < 11) {

                document.write(i + ",");

                i++;

            }

        }

    </script>

</head>

<body>

    <button onclick="MyFunction()">

        Click</button>

</body>

</html>

 

Output


foeloop.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

© 2020 DotNetHeaven. All rights reserved.