JavaScript For Loop

for loop in javascript

JavaScript For loop: Loops can execute a block of code a number of times. They are handy in cases in which you want to run the same code repeatedly, adding a different value each time.

JavaScript has three types of loops:

  • for loop
  • while loop
  • do-while loop

Java Script For loop

The for loop is commonly used when creating a loop.


Flow Chart


Example

The following example to learn how a for loop works in Java Script.


<html>

   <body>      

      <script type = "text/javascript">

         <!--

            var count;

            document.write("Starting Loop" + "<br />");

         

            for(count = 0; count < 10; count++) {

               document.write("Current Count : " + count );

               document.write("<br />");

            }         

            document.write("Loop stopped!");

         //-->

      </script>      

      <p>Set the variable to different value and then try...</p>

   </body>

</html>

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *