for loop

JavaScript have the for loop and has the same syntax like C# or Java. We can use the for loop for execute same code repeatedly. For loop is also known as the counter loop.

Syntax:

for (initializer; condition; increment / decrements) {

}

The for loop have the three arguments:

  • initializer: Here, we can define the one or more variables.
  • condition: Here we have to specify the condition, that will be checked every time by the for loop before the iteration. If the condition is true than and only than loop will be iterate otherwise it will terminated.
  • Increment/decrements: Here we have to increment or decrements any variables. It will help for the condition related task.

initializer statement will only initialize the variables and executed only one time no matter how much time loop is iterated or not.

condition statement is executed every time or till the condition is true. Because the loop’s iteration is based on the condition. once the condition is false than the for loop is terminated.

Increment/decrements statement is also execute as much as time loop is terminated. Here we can increment or decrements the value of the variable.

Example:

for (var i = 1; i < 11; i++) {
    console.log(i);
}

// 1 2 3 4 5 6 7 8 9 10

In above example loop will be iterate 10 times and print the 1 to 10 in the console. To see the result press the F12 (Developer tools). You will see the output in the console.

Execution flow of for loop:

  • Step 1 : Vary first time variable i is initialize with the assigning value. It means initializer statement executed first and executed only once.
  • Step 2 : After that condition is checked, If condition is true than the block of code is executed otherwise the loop is terminated.
  • Step 3 : If the condition is true and block of code executed, After it the increment or decrement statement is executed and the value of the variable increment or decrement based on the statement.
  • Step 4 : Go to step 2 till the condition is true.

Infinite for loop

we are also able to create the infinite loop in the for loop by using nothing to pass in it. Yes it is right, when you pass nothing to the loop than the loop become the infinite, So compulsory we have to use the break statement to break the infinite for loop.

var i = 0;
for ( ; ; ) {
    console.log(++i);
    if (i >= 10) {
        break;
    }
}

// 1 2 3 4 5 6 7 8 9 10

In above example, the infinite for loop is executing. And it will print the 1 to 10 in the console once the value of the i become the 11 than the break statement will be executed and loop will be terminated.

for loop on Array

We can also use the for loop on the array. Let’s create example that will print the value of the array in the console.

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

for (var i = 0; i < numbers.length; i++) {
    console.log(numbers[i]);
}

// 1 2 3 4 5 6 7 8 9 10

In above example, For loop will iterate the same numbers of the length of the array. In above loop we are finding the length of the loop and checked with the value of the i. If the value of is less than the length of the array than the loop will be executed otherwise the loop will be terminated.

We are using the i as the index of the array to get the value of the array and print it on the console.