JavaScript Roadmap — Day 12: Loops
Loops — for, while, for...of, for...in
Loops are how you make JavaScript do the same thing many times without repeating yourself. Master all five loop types and know exactly when to use each one.
Imagine you are building a shopping app. You need to display 100 products. Without loops your code would look like this:
A loop solves this in 3 lines. It repeats a block of code automatically — as many times as you need. This is one of the most fundamental concepts in all of programming. Every language has loops. Every program uses them.
1. Why Loops? — The Big Picture
JavaScript has 5 loop types. Each one is designed for a specific situation. Knowing which one to use — and when — is what separates beginners from professional developers:
| Loop | Best Used For | When to Use |
|---|---|---|
for |
Known number of times | Counting, indexes |
while |
Unknown number of times | Wait for condition |
do...while |
Run at least once | User input validation |
for...of |
Arrays and iterables | Modern array looping |
for...in |
Object properties | Loop over object keys |
2. for Loop — The Classic
The for loop is the most used loop in JavaScript. Use it when you know exactly how many times you want to repeat something. It has three parts — initializer, condition, and increment:
3. while Loop — When You Don't Know How Many Times
The while loop keeps running as long as a condition is true. Use it when you do not know in advance how many iterations you need — like waiting for user input or processing data until a condition is met:
🔥 Infinite Loop Warning: If the while condition never becomes false — your program freezes forever. Always make sure something inside the loop eventually makes the condition false. This is the most common beginner mistake with while loops.
4. do...while — Run at Least Once
The do...while loop is like while — but it always runs the code block at least once before checking the condition. Perfect for menus and input validation:
5. for...of — Modern Array Looping 🔥
for...of was added in ES6 and is now the preferred way to loop over arrays, strings, and other iterables. It is cleaner and more readable than the classic for loop for most use cases:
for...of is preferred over classic for loops for arrays. It is cleaner, more readable, and less error prone — no index management, no off-by-one errors. Use it whenever you do not need the index.for...of supports break and continue — forEach does not. If you need to stop early — use for...of. If you just need to run a function on each item — forEach is fine.for...of only works on iterables — arrays, strings, Maps, Sets. It does NOT work on plain objects. For objects use for...in or Object.entries() with for...of.6. for...in — Loop Over Object Properties
for...in loops over the keys of an object. It is specifically designed for objects — not arrays:
7. break & continue — Control Your Loops
Two keywords that give you fine-grained control over loop execution. break stops the loop completely. continue skips the current iteration and moves to the next one:
8. Try It Yourself
Edit the code and click Run Code. Try changing values and see how each loop behaves differently!
9. Practice Tasks
Task 1 — Easy: Multiplication Table
Use a for loop to print the multiplication table of any number from 1 to 10. Example for 5: "5 x 1 = 5", "5 x 2 = 10" ... "5 x 10 = 50".
Task 2 — Medium: Shopping Cart
Create an array of 5 products with name and price. Use for...of to loop through them. Use continue to skip products over Rs3000. Calculate and print the total of remaining products only.
Task 3 — Hard: FizzBuzz
Classic interview question — loop from 1 to 30. Print "Fizz" if divisible by 3, "Buzz" if divisible by 5, "FizzBuzz" if divisible by both, otherwise print the number. Use % operator and if/else inside a for loop.