๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท
โšก JavaScript

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.

5 min 10 XP Lesson 5 of 21
Loops
๐ŸŒ

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

  • โ€ขbreak exits any loop; continue skips 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

  • โ†’break exits any loop; continue skips 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

โ€ข Rendering a list of posts: <code>posts.forEach(post => renderCard(post))</code> โ€ข Calculating cart total: <code>let total = 0; for (const item of cart) { total += item.price; }</code> โ€ข Checking each notification: <code>for (const notif of notifications) { if (!notif.read) showBadge(); }</code> โ€ข Countdown timer: <code>for (let i = 10; i &gt; 0; i--) { tick(i); }</code>

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

1 / 2

In for (let i=0; i<3; i++), how many times does it run?