The for loop

The classic for loop is the workhorse of JavaScript iteration. It gives you full control: an initializer, a condition, and an update expression. You use it when you know exactly how many times you want to loop โ€” like iterating through every product in a catalog by index.

๐Ÿ›’
UrbanCart Example
Iterate through the product catalog by index

UrbanCart stores its product catalog in an array. A for loop lets you visit each product by index โ€” perfect for numbered lists, paginated displays, or when you need the position of each item.

for_loop.js Classic for loop โ€” catalog indexes
Console output
1. Wireless Mouse 2. Mechanical Keyboard 3. USB-C Hub 4. Laptop Stand 5. Webcam HD
let i = 0
Initialize
โ†’
i < length?
Check condition
โ†’
Run body
Execute code
โ†’
i++
Update
Key anatomy: for (let i = 0; i < array.length; i++) โ€” three parts separated by semicolons. The loop runs as long as the condition is true. When it becomes false, the loop stops.

The while loop

A while loop runs as long as its condition is true. It's ideal when you don't know in advance how many iterations you need โ€” like adding items to a cart until you run out of budget.

๐Ÿ’ฐ
UrbanCart Example
Add items to cart while total is under budget

A customer has a budget of 5000. They keep adding products to their cart until the next item would push them over budget.

while_loop.js Budget-based cart filling
Console output
Cart: Mouse, Keyboard, Cable, Monitor Total: โ‚น4596 Remaining budget: โ‚น404
Watch out for infinite loops! If the condition never becomes false, the loop runs forever and crashes your browser. Always make sure something inside the loop moves you closer to the exit condition.

The do...while loop

A do...while loop is just like while, but it always executes the body at least once before checking the condition. This is perfect when you must perform an action first and then decide whether to repeat โ€” like prompting a user at least once for input.

do_while.js Always execute at least once
Console output
Attempt 1: user entered 0 Attempt 2: user entered -1 Attempt 3: user entered 3 Valid quantity accepted: 3
๐Ÿ”„

while

Checks condition before running the body. May execute zero times if the condition is already false.

โ–ถ๏ธ

do...while

Runs the body first, then checks condition. Always executes at least once.

The for...of loop

Introduced in ES6, for...of is the cleanest way to loop through arrays (and other iterables like strings, Maps, and Sets). You get each value directly โ€” no index tracking needed.

for_of.js Modern array iteration
Console output
=== UrbanCart Product List === Wireless Mouse โ€” โ‚น799 Mechanical Keyboard โ€” โ‚น2499 USB-C Hub โ€” โ‚น1299 Laptop Stand โ€” โ‚น1899 Webcam HD โ€” โ‚น3499
When to use for...of: Prefer it over the classic for loop when you don't need the index. It's shorter, more readable, and less error-prone. Use the classic for when you need i for calculations or when iterating in reverse.

The for...in loop

for...in loops over the keys (property names) of an object. This is useful when you want to inspect every property of a product object โ€” its name, price, category, stock status, etc.

for_in.js Loop object keys
Console output
=== Product Details === name: Mechanical Keyboard price: 2499 brand: UrbanCart Essentials category: Peripherals inStock: true rating: 4.7
Don't use for...in on arrays! It iterates over property keys (which are strings, not numbers) and may include inherited properties. Use for...of or a classic for loop for arrays. Reserve for...in for plain objects.

break & continue

Sometimes you need to exit a loop early or skip certain iterations. That's where break and continue come in.

break โ€” stop when item found

Use break to exit a loop immediately. For example, stop searching the catalog once you find the product you're looking for:

break.js Stop searching when found
Console output
Checking index 0: Wireless Mouse Checking index 1: Mechanical Keyboard Checking index 2: USB-C Hub Found! Stopping search. USB-C Hub is at index 2

continue โ€” skip out-of-stock items

Use continue to skip the current iteration and move to the next one. For example, skip out-of-stock items when building a display list:

continue.js Skip out-of-stock items
Console output
=== Available Products === โœ“ Wireless Mouse โœ“ USB-C Hub โœ“ Webcam HD

Try it yourself โ€” Loop the catalog

Loop through the products array and display each product's name and price. Use any loop style you like โ€” for, for...of, or while. Click Run to see the output.

โœ๏ธ Try It Yourself โ€” Display Product Catalog
Output

Try it yourself โ€” Find the most expensive product

Write a loop that finds the product with the highest price. Track the current maximum as you iterate. Try solving it before looking at the starter code!

โœ๏ธ Try It Yourself โ€” Most Expensive Product
Output

Test your knowledge

Question 1 of 5
What are the three parts of a classic for loop header?
Question 2 of 5
What is the key difference between while and do...while?
Question 3 of 5
Which loop should you use to iterate over an array's values in modern JavaScript?
Question 4 of 5
What does the break statement do inside a loop?
Question 5 of 5
Why should you avoid using for...in on arrays?
0
/5
Great job!

๐Ÿ† Bonus Challenge

Combine what you've learned! Write a program that:

  • Loops through an array of UrbanCart products (each with name, price, and inStock)
  • Uses continue to skip out-of-stock items
  • Uses break to stop once you've found 3 affordable items (price under 2000)
  • Logs each affordable, in-stock item as you find it

Try it in the editors above, or open your browser's DevTools console (F12) and write it from scratch!

Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

You just mastered JavaScript loops.
Now build a full-stack career.

Join the Best AI Programs โ€” three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses โ†’