Q:

for loop js example


for (i = 0; i < 5; i++) {

   text += "The number is " + i + "<br>";

}

  
5
let array = ['Item 1', 'Item 2', 'Item 3'];

for (let index = 0; index < array.length; index++) {
  console.log("index", index);
  console.log("array item", array[index]);
}
10


for (statement 1; statement 2; statement 3) {

      // code block to be executed

 }

 
2
var colors=["red","blue","green"];
for(let color of colors){
  console.log(color)
}
6

 var i = 2;
var len = cars.length;
var text = "";
for (; i < len; i++) { 

    text += cars[i] + "<br>";

 } 
2

for (i = 0, len = cars.length, text = ""; i < len; i++) { 
  text += cars[i] + "<br>";

 } 
0
let txt = 'JavaScript';
for (let x of txt) { 
  document.write(x + "<br >");
}
let cars = ['BMW', 'Volvo', 'Mini'];
for (let x of cars) {
   document.write(x + "<br >");
} 
0

New to Communities?

Join the community