JavaScript – For Loop

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.

Flow Chart

The flow chart of a for loop in JavaScript would be as follows −

For Loop

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

Try the following example to learn how a for loop works in JavaScript.

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

Output

Starting Loop
Current Count : 0
Current Count : 1
Current Count : 2
Current Count : 3
Current Count : 4
Current Count : 5
Current Count : 6
Current Count : 7
Current Count : 8
Current Count : 9
Loop stopped! 
Set the variable to different value and then try...

Related Posts

  • 91
    While writing a program, you may encounter a situation where you need to perform an action over and over again. In such situations, you would need to write loop statements to reduce the number of lines. JavaScript supports all the necessary loops to ease down the pressure of programming. The…
    Tags: count, loop, current, tutorial, java, script, javascript
  • 72
    Let us take a simple expression 4 + 5 is equal to 9. Here 4 and 5 are calledoperands and ‘+’ is called the operator. JavaScript supports the following types of operators. Arithmetic Operators Comparision Operators Logical (or Relational) Operators Assignment Operators Conditional (or ternary) Operators Lets have a look…
    Tags: javascript, tutorial, script, java
  • 60
    What is an Event ? JavaScript's interaction with HTML is handled through events that occur when the user or the browser manipulates a page. When the page loads, it is called an event. When the user clicks a button, that click too is an event. Other examples include events like…
    Tags: script, javascript, java
  • 53
    Let us take a sample example to print out "Hello World". We added an optional HTML comment that surrounds our JavaScript code. This is to save our code from a browser that does not support JavaScript. The comment ends with a "//-->". Here "//" signifies a comment in JavaScript, so…
    Tags: javascript, script
  • 48
    The for...in loop is used to loop through an object's properties. As we have not discussed Objects yet, you may not feel comfortable with this loop. But once you understand how objects behave in JavaScript, you will find this loop very useful. Syntax for (variablename in object){ statement or block…
    Tags: loop, javascript

Leave a Reply

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

%d bloggers like this: