JavaScript in 60 Seconds

For loop

What is a for loop?

A loop allows you to repeat an action based on some condition, similar to an if statement. In the prior lesson you met the while loop. The for loop is similar…

for (var i=0; i<9; i++) { // do this each cycle } // is the same as var i = 0; while (i<9) { i++; // same as i=i+1; }

Let’s break it down

For loops can make it a little easier to avoid infinite loops.

for (beforeTheLoopStarts; condition; afterEachCycle) { } for (let x = 100; x > 10; x--) { } i--; // same as i=i-1;

So…
let x = 100; - Before the loop runs
x > 10; - Condition to check BEFORE each cycle. if true, run the cycle
x-- - AFTER each cycle run this. In this case decrement by 1.