JavaScript Loops
for (let i = 1; i <= 3; i++) console.log("for", i);
let n = 3;
while (n > 0) { console.log("while", n); n--; }
const fruits = ["apple", "mango"];
for (const f of fruits) console.log("of", f); // values
const marks = { math: 90, sci: 85 };
for (const k in marks) console.log("in", k, marks[k]); // keys
for 1
for 2
for 3
while 3
while 2
while 1
of apple
of mango
in math 90
in sci 85
| Loop | Iterates over |
|---|---|
| for…of | Values of iterables (arrays, strings, Maps, Sets) |
| for…in | Enumerable property keys of an object |
| array.forEach | Each element, with a callback |
Track your progress through the JavaScript course