for loop for javascript code example

Example 1: javascript for loop

var colors=["red","blue","green"];
for (let i = 0; i < colors.length; i++) { 
  console.log(colors[i]);
}

Example 2: for loop javascript

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

Example 3: loop javascript

// infinite for loop
for(let i = 1; i > 0; i++) {
    // block of code
}

Example 4: javascript for in loop

const p = {
    "p1": "value1",
    "p2": "value2",
    "p3": "value3"
};

for (let [key, value] of Object.entries(p)) {
  console.log(`${key}: ${value}`);
}