What is a loop — and why it can run zero times

Loops usually get explained like this: "code that repeats N times." Sounds clear — and gets in your way. Because in real code you almost never know that N in advance.
How many items are in the cart? How many messages arrived? How many rows in the uploaded file? The answer is always the same: however many there are. And from that follows a thing that surprises people: the body of a loop can run zero times — and that's not a bug, that's it working correctly.
What a loop is
A loop is how you say "do the same thing for every item" without hand-writing the code for each item.
Without a loop you write:
console.log("tea")
console.log("honey")
console.log("lemon")
Three lines for three products. And for three hundred? That's where the loop shows up:
for (const item of cart) {
console.log(item)
}
It reads almost like English: "for each item of cart — print it." One piece of code, any number of products. A fourth one shows up — you change nothing.
Notice where the number of repeats comes from. You never wrote it. It equals the length of the array — as many items, as many rounds.
Three kinds of loop you'll see in AI-written code
There are formally more, but these are the ones that show up.
for...of— walk a list. The most common one. "For each item — do this." The repeat count comes from the list's length.forwith a counter —for (let i = 0; i < 10; i++). Here you set the number yourself. Useful when the step number matters more than the item.while— keep going while a condition holds. The repeat count is unknown even at runtime: "while there are more pages", "while the server says there's more".
There's also map and forEach — also list walks, just written shorter. When items.map(...) shows up in code, there's a loop inside it, merely hidden.
Why zero repeats is normal
Here's the thing worth taking away.
const cart = []
for (const item of cart) {
console.log(item) // never reached
}
No error. No warning. The loop honestly looked at the list, saw zero items and finished having done nothing.
That explains the most common beginner complaint: "the screen is blank but the console is clean." It's supposed to be clean. The code ran perfectly — there was simply zero data. When that happens, look for the cause not in the loop but in the list itself: either it hasn't arrived yet, or a filter drained it.
The same fact gives you a practical rule. Anything that must happen at least once can't live inside a list loop. A section heading, a "nothing found" line, closing a connection — none of them belong in a body that might never run.
When a loop won't stop
The opposite trouble: a loop that spins forever and freezes the tab. Almost always it's a while whose condition never changes:
let page = 1
while (page <= total) {
loadPage(page)
// nobody increments page — the condition stays true forever
}
The eyeball check is simple: find the line in the body that moves the loop toward its exit. No such line, infinite loop. for...of doesn't get this disease: the list is finite, the walk ends by itself.
Worth keeping separate from the "agent loop". When an AI agent keeps calling tools round after round until the task is done, that's an agent loop — a different story with different reasons for hanging.
Loops and async — where everyone trips
There's a trap that catches everyone who asks AI to "walk the list and load the data".
A loop can't wait on its own. If every round contains an async operation — a server request, a file read — then a plain forEach fires all the requests at once and races ahead without waiting for the answers. You get an empty result and no error at all.
The fix is to write the waiting explicitly: for...of together with await honestly waits out each round. Practical move: when you ask AI for that kind of code, add "wait for each request before the next one" to the prompt. Short line, saves you an evening of debugging.
How is for different from while?
The repeat count. for...of walks a ready-made list — the end is known upfront. while spins while a condition is true and doesn't know the end in advance. When in doubt, take for...of: it can't run away from you.
Do loops slow an app down?
By themselves, almost never: a processor walks a million simple steps instantly. What's slow is something else — a server or database call inside the loop, turning a hundred items into a hundred requests. The good move is to pull the request out and fetch all the data in one go.
Short story-lessons, an agent simulator and daily practice — in our mobile app. Free.





