For Loops in JavaScript

Iterate over things in JavaScript with for loops using the following syntax:

for (var i = 0; i < 10; i++) {
  console.log("Iteration " + i);

var i = 0

The initial statement, which is executed before the loop starts. Usually you’ll use this to declare a counter. You’ll see a lot of code with just i = 0, but it’s a good practice to use the var keyword to ensure that the variable is declared locally.

i < 0

The condition statement. The loop will run until the condition becomes false. Often the condition will look similar to something like this to run through all the items of an array:

i < myArray.length


The iteration statement. Iterate on something that will eventually make the condition false.

  Tweet It

🕵 Search Results

🔎 Searching...