It is generally common knowledge that computers are great at performing repetitive tasks an infinite number of times, and doing so very quickly. It is also common knowledge that computers really don't do anything unless someone programs them to tell them what to do. Loop statements are the primary mechanism for telling a computer to perform the same task over and over again until a set of criteria are met. This is where for, while and do ... while loops are of use.
JavaScript includes for loop like Java or C#. Use for loop to execute code repeatedly.
Syntax:
for(initializer; condition; iteration)
{
// Code to be executed
}
The for loop requires following three parts.
- Initializer: Initialize a counter variable to start with
- Condition: specify a condition that must evaluate to true for next iteration
- Iteration: increase or decrease counter
Example: for loop
for (var i = 0; i < 5; i++)
{
console.log(i);
}
Output:
0 1 2 3 4
In the above example, var i = 0 is an initializer statement where we declare a variable i with value 0. The second part, i < 5 is a condition where it checks whether i is less than 5 or not. The third part, i++ is iteration statement where we use ++ operator to increase the value of i to 1. All these three parts are separated by semicolon ;.
The for loop can also be used to get the values for an array.
Example: for loop
var arr = [10, 11, 12, 13, 14];
for (var i = 0; i < 5; i++)
{
console.log(arr[i]);
}
Output:
10 11 12 13 14
Please note that it is not mandatory to specify an initializer, condition and increment expression into bracket. You can specify initializer before starting for loop. The condition and increment statements can be included inside the block.
Example: for loop
var arr = [10, 11, 12, 13, 14];
var i = 0;
for (; ;) {
if (i >= 5)
break;
console.log(arr[i]);
i++;
}
Output:
10 11 12 13 14
Learn about while loop in the next section.

- JavaScript for loop is used to execute code repeatedly.
- for loop includes three parts: initialization, condition and iteration. e.g.
for(initializer; condition; iteration){ ... }
- The code block can be wrapped with { } brackets.
- An initializer can be specified before starting for loop. The condition and increment statements can be included inside the block.
while loop
JavaScript includes while loop to execute code repeatedly till it satisfies a specified condition. Unlike for loop, while loop only requires condition expression.
Syntax:
while(condition expression)
{
/* code to be executed
till the specified condition is true */
}
Example: while loop
var i =0;
while(i < 5)
{
console.log(i);
i++;
}
Output:
0 1 2 3 4
As you can see in the above example, while loop will execute the code block till i < 5 condition turns out to be false. Initialization statement for a counter variable must be specified before starting while loop and increment of counter must be inside while block.
do while
JavaScript includes another flavour of while loop, that is do-while loop. The do-while loop is similar to while loop the only difference is it evaluates condition expression after the execution of code block. So do-while loop will execute the code block at least once.
Syntax:
do{
//code to be executed
}while(condition expression)
Example: do-while loop
var i = 0;
do{
alert(i);
i++;
} while(i < 5)
Output:
0 1 2 3 4
The following example shows that do-while loop will execute a code block even if the condition turns out to be false in the first iteration.
Example: do-while loop
var i =0;
do{
alert(i);
i++;
} while(i > 1)
Output:
0

- JavaScript while loop & do-while loop execute the code block repeatedly till conditional expression returns true.
- do-while loop executes the code at least once even if condition returns false.