for Loop in JavaScript
The classic for loop packs initialization, condition, and step into one header: for (let i = 0; i < n; i++). It is the precision tool for counted repetition — indexed traversal, fixed iterations, stepped sequences — with everything controlling the loop in one line.
Declaring the counter with let scopes it to the loop (each iteration gets a fresh binding — closures inside capture correctly, unlike legacy var). Any header slot may empty out, but an empty condition means infinite.
Three slots, one line
Init runs once, condition tests before every pass, step runs after each body.
let scopes the counter
for (let i ...) confines i per-iteration — closures created inside capture the right value automatically.
Stepping and bounds
i += 2 strides, i-- counts down; i < arr.length matches zero-based indexing.
Empty slots are legal
for (;;) loops forever — pair with break, or fill all slots for clarity.
let sum = 0;for (let i = 1; i <= 5; i++) { sum += i;}console.log(sum);One pass over n iterations with constant extra state.
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.