Loops
๐ What are loops in JavaScript? They repeat a block of code. A classic `for` loop looks like `for (let i = 0; i < n; i++)` and runs a counted number of times. You can also loop arrays with `for...of` or use `while` when you want to keep going until something changes.

Appy Saysโฆ
JavaScript has five types of loops โ more than Python. The most modern and cleanest are for...of and .forEach(). Knowing when to use which one is a mark of experience.
Loops in JavaScript
JavaScript has several loop types. The two you'll use most in modern code:
- โข
for (let i = 0; i < n; i++)โ classic counter loop - โข
for (const item of array)โ loop through every item (cleaner, preferred for arrays) - โข
array.forEach(item => { ... })โ modern functional style (same idea as for...of) - โข
while (condition)โ repeat until condition is false - โข
for (const key in object)โ loop through an object's keys
for vs for...of โ index vs item
Imagine a playlist. for (let i = 0; i < playlist.length; i++) is like saying 'give me song at position 0, then position 1, then position 2โฆ'. for (const song of playlist) is like saying 'give me each song, one at a time' โ cleaner and less error-prone.
Real-World Examples
- โขRendering a list of posts:
posts.forEach(post => renderCard(post)) - โขCalculating cart total:
let total = 0; for (const item of cart) { total += item.price; } - โขChecking each notification:
for (const notif of notifications) { if (!notif.read) showBadge(); } - โขCountdown timer:
for (let i = 10; i > 0; i--) { tick(i); }
Key Facts
- โข
breakexits any loop;continueskips to the next iteration - โข
array.map(fn)loops and returns a new array โ used constantly in React - โข
array.filter(fn)loops and returns items where fn returns true - โข
array.reduce(fn, initial)loops to compute a single value (like a total) - โขThese array methods (
map,filter,reduce) are considered modern best practice over raw loops
Remember
In modern JavaScript / React code, you'll see .map() far more often than for loops. It's the go-to for transforming a list of data into a list of UI elements. Get comfortable with it early.
What You Learned
- โข
for (let i = 0; i < n; i++)โ classic counter loop - โข
for (const item of array)โ preferred for iterating arrays - โข
array.forEach(item => { })โ modern functional style - โข
while (condition)โ repeat until condition is false - โข
map / filter / reduceโ powerful array methods used in real apps
Key Facts
- โ
breakexits any loop;continueskips to the next iteration - โ
array.map(fn)loops and returns a new array โ used constantly in React - โ
array.filter(fn)loops and returns items where fn returns true - โ
array.reduce(fn, initial)loops to compute a single value (like a total) - โThese array methods (
map,filter,reduce) are considered modern best practice over raw loops
Real-World Examples
Remember
In modern JavaScript / React code, you'll see .map() far more often than for loops. It's the go-to for transforming a list of data into a list of UI elements. Get comfortable with it early.
Quick Quiz
In for (let i=0; i<3; i++), how many times does it run?