How to use Loops in Javascript

Loops are useful when you have to execute the same lines of code repeatedly, for a specific number of times or as long as a specific condition is true. Suppose you want to type a ‘Hello’ message 100 times in your webpage. Of course, you will have to copy and paste the same line 100 times. Instead if you use loops, you can complete this task in just 3 or 4 lines.
Different Types of Loops
There are mainly four types of loops in JavaScript.
  1. for loop
  2. for/in loop (explained later)
  3. while loop
  4. do…while loop

for loop

Syntax:
?
1
2
3
4
5
6
7
for(statement1; statement2; statment3)
 
{
 
lines of code to be executed
 
}
  1. The statement1 is executed first even before executing the looping code. So, this statement is normally used to assign values to variables that will be used inside the loop.
  2. The statement2 is the condition to execute the loop.
  3. The statement3 is executed every time after the looping code is executed.
Try this yourself:
This code is editable. Click Run to Execute
1
2
    <html>
3
4
    <head>
5
6
    <script type="text/javascript">
7
8
    var students = new Array("John", "Ann", "Aaron", "Edwin", "Elizabeth");
9
10
    document.write("<b>Using for loops </b><br />");
11
12
    for (i=0;i<students.length;i++)
13
14
    {
15
16
    document.write(students[i] + "<br />");
17
18
    }
19
20
    </script>
21
22
    </head>
23
24
    <body>
25
26
    </body>
27
28
    </html>
29

while loop

Syntax:
?
1
2
3
4
5
6
7
while(condition)
 
{
 
lines of code to be executed
 
}
The “while loop” is executed as long as the specified condition is true. Inside the while loop, you should include the statement that will end the loop at some point of time. Otherwise, your loop will never end and your browser may crash.
Try this yourself:
This code is editable. Click Run to Execute
1
2
    <html>
3
4
    <head>
5
6
    <script type="text/javascript">
7
8
    document.write("<b>Using while loops </b><br />");
9
10
    var i = 0, j = 1, k;
11
12
    document.write("Fibonacci series less than 40<br />");
13
14
    while(i<40)
15
16
    {
17
18
    document.write(i + "<br />");
19
20
    k = i+j;
21
22
    i = j;
23
24
    j = k;
25
26
    }
27
28
    </script>
29
30
    </head>
31
32
    <body>
33
34
    </body>
35
36
    </html>
37

do…while loop

Syntax:
?
1
2
3
4
5
6
7
do
 
{
 
block of code to be executed
 
} while (condition)
The do…while loop is very similar to while loop. The only difference is that in do…while loop, the block of code gets executed once even before checking the condition.
Try this yourself:
This code is editable. Click Run to Execute
1
2
    <html>
3
4
    <head>
5
6
    <script type="text/javascript">
7
8
    document.write("<b>Using do...while loops </b><br />");
9
10
    var i = 2;
11
12
    document.write("Even numbers less than 20<br />");
13
14
    do
15
16
    {
17
18
    document.write(i + "<br />");
19
20
    i = i + 2;
21
22
    }while(i<20)
23
24
    </script>
25
26
    </head>
27
28
    <body>
29
30
    </body>
31
32
    </html>
33