🔄
Intermediate⏱ 12 min#04 / 08

Loops

for, while & Iteration

Loops let you repeat code multiple times without writing it over and over. JavaScript has several loop types - each optimized for different situations.

Your Progress0/8 complete
0 visited0%
💡 Real-World Analogy

A loop is like a Ferris wheel - it goes round and round, each rotation doing the same thing but with different passengers (values). It stops when you decide (condition becomes false or break is called).

🔑 Key Concepts

for loop

Best when you know exactly how many iterations you need. Compact init + condition + increment.

while / do-while

while checks condition before each run. do-while always runs at least once then checks.

for...of / forEach

Modern, clean iteration over arrays & iterables. forEach gives index too.

💻 Code Examples

1for Loop
loops.js
1// Structure: for (init; condition; increment)
2for (let i = 0; i < 5; i++) {
3 console.log("Iteration " + i);
4} // prints 0, 1, 2, 3, 4
5 
6// Count down
7for (let i = 5; i > 0; i--) {
8 console.log("Countdown: " + i);
9} // 5, 4, 3, 2, 1
10 
11// Loop array by index
12let fruits = ["Apple", "Banana", "Cherry"];
13for (let i = 0; i < fruits.length; i++) {
14 console.log((i + 1) + ". " + fruits[i]);
15}
16 
17// Step by 2
18for (let i = 0; i <= 10; i += 2) {
19 console.log(i); // 0, 2, 4, 6, 8, 10
20}
2while & do-while
loops.js
1// while - checks condition BEFORE each iteration
2let lives = 3;
3while (lives > 0) {
4 console.log("Lives: " + lives);
5 lives--;
6} // 3, 2, 1
7 
8// do-while - runs AT LEAST ONCE
9let attempt = 0;
10do {
11 attempt++;
12 console.log("Attempt #" + attempt);
13} while (attempt < 3);
14// #1, #2, #3
15 
16// break and continue
17for (let i = 0; i < 10; i++) {
18 if (i === 3) continue; // skip 3
19 if (i === 7) break; // stop at 7
20 console.log(i); // 0, 1, 2, 4, 5, 6
21}
3Modern: for...of & forEach
loops.js
1let heroes = ["Batman", "Spider-Man", "Iron Man"];
2 
3// for...of - cleanest for arrays
4for (let hero of heroes) {
5 console.log(hero);
6}
7 
8// forEach - array method with index
9heroes.forEach((hero, i) => {
10 console.log((i + 1) + ". " + hero);
11});
12 
13// for...in - iterate object keys
14let scores = { math: 95, english: 88, science: 92 };
15for (let subject in scores) {
16 console.log(subject + ": " + scores[subject]);
17}
18 
19// Nested loops - multiplication table
20for (let i = 1; i <= 3; i++) {
21 for (let j = 1; j <= 3; j++) {
22 console.log(i + " x " + j + " = " + (i * j));
23 }
24}

⚠️ Common Mistakes

❌ Mistake

Forgetting to increment - infinite loop crash!

✅ Fix

Always ensure the loop condition eventually becomes false. i++ is your friend.

❌ Mistake

i <= array.length (off-by-one error)

✅ Fix

Arrays are 0-indexed, so the last index is length-1. Use i < array.length.

❌ Mistake

Using for...in on arrays

✅ Fix

for...in is for OBJECTS (iterates keys as strings). Use for...of or forEach for arrays.

📋 Quick Reference

for(init; cond; inc) {}while(cond) {}do {} while(cond)for(item of array) {}array.forEach(fn)break - exit loopcontinue - skip iterationfor(key in obj) {}

Live Code Playground

Edit the code and press ▶ Run to see output instantly.

loops_playground.js
⟳ Loading editor...
💾 Auto-saved
// Press ▶ Run Code to execute and view output console logs...
🔢
← Previous
Math Functions
Challenge Mode
Next →
Arrays
📋