JavaScript Loops

Loops can execute a block of code a number of times. if you want to run the same code over and over again, each time with a different value.

Instead of writing:

text += bikes[0] + "<br>"; 
text += bikes[1] + "<br>"; 
text += bikes[2] + "<br>";
text += bikes[3] + "<br>";
text += bikes[4] + "<br>";
text += bikes[5] + "<br>";

You can write:

for (i = 0; i     text +=bikes[i] + "


JavaScript supports different kinds of loops:

  • for 
  • for/in 
  • while 
  • do/while 

The For Loop In JavaScript

The for loop is often the tool you will use when you want to create a loop.


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


The For/In Loop In JavaScript

The JavaScript for/in statement loops through the properties of an object:


var person = {fname:"John", lname:"Doe", age:25}; 

var text = "";
var x;
for (x in person) {
    text += person[x];


Working hours


Day Timing
Monday To Friday 8:00 Am To 8:00Pm
Saturday & Sunday 8:00 Am To 5:00Pm

Contact Information
ADDRESS: 263, First Floor
Pocket H-17, Sector 7,
Rohini, Delhi - 110085
Near Rohini West Metro Station
Opposite Metro Pillar No. 425
Ansal Sushant City, Kundli,
Distt. Sonepat


Mobile: 9891 222 738, 9891 50 1300


Copyright © 2014 Bapu Graphics, All Right Reserved.