How to use For loop in JavaScript

In this article I am going to explain about for loop in JavaScript.
  • 2103

JavaScript For loop

We use loop when we want to execute any code over and over again till specified number of time.

  • For loop execute code specified number of time
  • We use for loop when we know code how many time should be run.
  • In For loop firstly initialized variable, then condition and in last increment.

For loop syntax

for(Variable= setvalue; Variable> condition; Variable+ increment)

        {

        //our code that that should be run

        }

In this example it will display 1 to 10 natural number

<html>

<head>

    <script type="text/javascript">

 

                      function MyFunction() {

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

                document.write(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

© 2019 DotNetHeaven. All rights reserved.