What is for loop statement in JavaScript?

JavascriptWeb DevelopmentFront End Technology

The 'for' loop is the most compact form of looping. It includes the following three important parts −

  • The loop initialization where we initialize our counter to a starting value. The initialization statement is executed before the loop begins.

  • The test statement which will test if a given condition is true or not. If the condition is true, then the code given inside the loop will be executed, otherwise the control will come out of the loop.

  • The iteration statement where you can increase or decrease your counter.

You can put all the three parts in a single line separated by semicolons.

Syntax

The syntax of for loop is JavaScript is as follows,

for(initialization;test condition;iteration statement){
   Statement(s)to be executed if test condition is true
}

Example

You can try to run the following example to learn how a for loop works in JavaScript −

Live Demo

<html>
   <body>
      <script>
         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>
   </body>
</html>
raja
Published on 25-Jan-2018 15:35:54
Advertisements