Loops
Repeat actions efficiently. The for, while, and do...while loop types.
Why Loops?
Avoid repetition. Let the computer do the work.
Without loops
console.log(1); console.log(2); console.log(3); console.log(4); console.log(5); // ... tedious!
With a loop
for (let i = 1; i <= 5; i++) {
console.log(i);
}
// Done! Works for 5
// or 5,000,000A loop repeats a block of code while a condition is true. When the condition becomes false, the loop stops.
for Loop
When you know how many times to repeat.
for (initialization; condition; update) {
// code to repeat
}
// Example:
for (let i = 0; i < 5; i++) {
console.log(i);
}
// Output: 0, 1, 2, 3, 41. Init
let i = 0
Runs once
2. Condition
i < 5
Checked each loop
3. Update
i++
After each loop
// Common for loop patterns:
// Count down
for (let i = 10; i > 0; i--) {
console.log(i); // 10, 9, 8, ... 1
}
// Step by 2
for (let i = 0; i < 10; i += 2) {
console.log(i); // 0, 2, 4, 6, 8
}
// Loop through array
const fruits = ["apple", "banana", "cherry"];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}while Loop
When you don't know how many iterations ahead of time.
while (condition) {
// code repeats as long as condition is true
}
// Example: keep doubling until >= 100
let n = 1;
while (n < 100) {
n = n * 2;
}
console.log(n); // 128while (n < 100) { n = n * 2; }
⚠️ Infinite loop danger! If the condition never becomes false, the loop runs forever and crashes the browser. Always ensure the condition will eventually be false.
do...while
Executes at least once, then checks the condition.
do {
// code runs AT LEAST once
} while (condition);
// Example: ask until valid input
let input;
do {
input = prompt("Enter a number > 0:");
} while (input <= 0);
// Example: generate random until match
let dice;
do {
dice = Math.ceil(Math.random() * 6);
} while (dice !== 6);while
1. Check condition
2. If true → run body
3. Go to step 1
May run 0 times
do...while
1. Run body
2. Check condition
3. If true → go to step 1
Always runs at least 1 time
// When condition is initially false:
let x = 10;
while (x < 5) {
console.log("while:", x); // never runs
}
do {
console.log("do-while:", x); // runs once!
} while (x < 5);for...of
Iterate over values in arrays, strings, maps, and more.
const colors = ["red", "green", "blue"];
for (const color of colors) {
console.log(color);
}
// "red"
// "green"
// "blue"Gives you each value directly — no index needed.
💡 for...of works with any iterable: Arrays, Strings, Maps, Sets, generators, NodeLists, etc.
for...in
Iterate over object keys (property names).
const user = { name: "Ada", age: 36, city: "London" };
for (const key in user) {
console.log(key, "→", user[key]);
}
// "name" → "Ada"
// "age" → 36
// "city" → "London"⚠️ for...in also iterates over inherited properties. Use Object.keys(obj) for own properties only.
for...of
Iterates values
Arrays, Strings, Maps
for...in
Iterates keys
Objects (properties)
// ⚠️ Don't use for...in with arrays:
const arr = ["a", "b", "c"];
for (const i in arr) {
console.log(typeof i); // "string" — keys are always strings!
}
// Use for...of for arrays instead.Which Loop to Use?
A quick decision guide.
| Scenario | Use |
|---|---|
| Known number of iterations | for |
| Unknown iterations, check first | while |
| Must run at least once | do...while |
| Array/iterable values | for...of |
| Object properties | for...in |
💡 In modern JavaScript, for...of and array methods (.forEach(), .map()) are preferred for iterating arrays. Use for when you need the index or complex iteration logic.
Exercises
Test your understanding.
How many times does this loop run?
for (let i = 0; i < 5; i++) {
console.log(i);
}What is the last value of i printed?
for (let i = 10; i >= 0; i -= 2) {
console.log(i);
}Which loop always runs at least once?
What does for...of iterate over?
Frequently Asked Questions
Common questions about loops.