advanced
 
 

SocialTwist Tell-a-Friend

Introduction Syntax Placement Variables Operators Popup Boxes If Else Statement Switch Case while Loop for Loop break continue Function Array DOM Events Date String Form Validation

JavaScript : for Loop

for Loop is the most common loop used in Javascript, when you need to perform some action over and over again for a given number of times.

for Loop
for Loop is used to execute the peice of code repeatedly for a specific number of times. In Javascript it looks like the following:

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

  • 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 the given condition is true or not. If condition is true then code given inside the loop will be executed otherwise loop will come out.
  • The iteration statement where you can increase or decrease your counter.
    You can put all the three parts in a single line separated by a semicolon.
    For example,
    <html>
    <head>
    <script type="text/javascript">
    <!--
    function forLoop() {
    var a;
    document.write("Starting for Loop" + "<br />");
    for (a=0; a < 10; a++){
    document.write("Current Count : " + a + "<br />");
    }
    document.write("for Loop stopped!");
    
    }
    //-->
    </script>
    </head>
    <body onload="forLoop()">
    
    </body>
    </html>
    
    

  • Previous            Next
    Contact Us
    Your Ad Here