For Loop

The standard for loop.

Examples

Basic For Loop

Looping 5 times (0 to 4).

for (let i = 0; i < 5; i++) {
  console.log("The number is " + i);
}

Looping with Arrays

Iterating through array items.

const cars = ["BMW", "Volvo", "Saab", "Ford"];
let text = "";
for (let i = 0; i < cars.length; i++) {
  text += cars[i] + "\n";
}
console.log(text);

Omitting Statements

Statements can be omitted if values are set elsewhere.

let i = 0;
let len = cars.length;
for (; i < len; ) {
  text += cars[i] + "\n";
  i++;
}

Test Your Knowledge

JavaScript Quiz

No quiz available for this topic yet.