JavaScript loops

• for

• while

• do-while

• for ... of

• for ... in

• forEach

 

FOR LOOPS

for (var i=0; i<cars.length; i++) { 

     document.write(cars[i] + "<br>");

}

 


FOR IN LOOPS

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

for (var key in person) {
    txt=txt + person[key];
}

// txt is John Doe 25

 

For ...of loops


let arr = [1,2,3]

for (let val of arr) {
   console.log(val)
}

 

forEach

let foo = [1,2,3]


foo.forEach( x => {
  console.log(x)
} )


// Note: does not allow break

 

While loops