JavaScript for Loop

JavaScript for loop check condition and executes block of code for a specific number of times, until specified condition argument become false.

JavaScript for loop accept 3 arguments separated by semicolons. All 3 arguments are optional.

Syntax

for (initialization; condition; iteration) {
    statements;     // Do stuff if for loop condition is true
}

Arguments

  1. initialization: var keyword to define and initialize counter variable. you can initializes the values of one or more counter variables. One or more counter variables separated by colon.
  2. condition: Check the condition expression, for given condition true or not. If the condition true, execute the block of code otherwise terminates the for loop.
  3. iteration: last end of loop iteration counter variable are evaluated. Here you can specify step of increment/decrement, default one.

Example: Following for loop example execute block of code until num value become 10. When num value become 11 the condition no longer satisfy and finally skip to execute for block.

<script>
    for( var num = 1; num <= 10; num++ ){
        document.writeln(num + ": iteration");
    }
    document.writeln("End of for loop");
</script>

Run it...   »

Example Result

Array iteration (JavaScript for Loop)

Example 1

<script>
    var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    for ( var num = 0; num < arr.length; num ++ ) {
        document.writeln(num + " iterate");
    }
    document.writeln("End of array for loop");
</script>

Run it...   »

Example Result

Example 2 we can little optimization for loop, length of the array store in variable and avoid to calculating length of array every iteration.

<script>
    var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    for ( var num = 0, size = arr.length; num < size; num++ ) {
        document.writeln(num + " iterate");
    }
    document.writeln("End of array for loop");
</script>

Run it...   »

Example Result

JavaScript for Loop arguments: optional

JavaScript for loop all three arguments are as an optional. Let's see one bye one without specified optional arguments,

Example 1 : Initialization arguments are as an optional.

<script>
    var num = 1;
    for(; num <= 10; num++){
        document.writeln(num + ": iteration");
    }
    document.writeln("End of for loop");
</script>

Run it...   »

Example Result :

Example 2: Condition arguments are as an optional. break; keyword indicates to terminate for loop condition.

<script>
    var num = 1;
    for(; ; num++){
        if (num <= 10)
            document.writeln(num + ": iteration");
        else
            break;
    }
    document.writeln("End of for loop");
</script>

Run it...   »

Example Result

Example 3: last iteration arguments are as an optional.

<script>
    var num = 1;
    for( ; ; ){
        if (num <= 10)
            document.writeln(num + ": iteration");
        else
            break;
        num++;
    }
    document.writeln("End of for loop");
</script>

Run it...   »

Example Result