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 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.
Initialize
Check condition
Execute code
Update
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.
A customer has a budget of 5000. They keep adding products to their cart until the next item would push them over budget.
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.
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: 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 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:
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:
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 โ 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!
Test your knowledge
for loop header?while and do...while?break statement do inside a loop?for...in on arrays?๐ Bonus Challenge
Combine what you've learned! Write a program that:
- Loops through an array of UrbanCart products (each with
name,price, andinStock) - Uses
continueto skip out-of-stock items - Uses
breakto 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!
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 โ