for, for/in, while, do/while loop

-- for loop

for (statement 1 ; statement 2 ; statement 3 ) { } //statement 1 - Executes before the loop //statement 2 - Condition to run the loop //statement 3 - Executes each time after the loop //See below for example for ( var i = 0 ; i < 10 ; i ++ ) { console .log (i); //Will print from 0 to 9 }

-- for/in loop to loop through an array or object

for ( var x in array or object) { } //Looping through an array var foo = [ 1 , 2 , 3 , 4 , 5 ]; for ( var i in foo) { console .log (foo[i]); //Will loop the array and prints 1 to 5 } //Looping through an object var obj = { fname : ' gokul ' , lname : ' krishh ' }; for ( var name in obj) { //To check property belongs to object, not to prototype object //hasOwnProperty is only to check objects if (obj.hasOwnProperty(name)) { console .log (obj[name]); //Will print "gokul" and "krishh" } }

-- while loop

while (statement 1 ) { } //statement 1 - Condition to run the loop //See below for example var i = 0 ; while (i < 10 ) { i ++ ; //Increment i console .log (i); //Will print from 1 to 10 }

-- do/while loop will execute once, before checking the condition in while