Initialization
Creates the counter, usually let index = 0.
for loops repeat a block with a counter. They are useful when you know how many steps you need or when indexes matter.
const items = ["HTML", "CSS", "JavaScript"];
for (let index = 0; index < items.length; index += 1) {
console.log(`${index + 1}. ${items[index]}`);
}
Control Flow
A classic for loop has three parts: initialization, condition and afterthought. The initialization runs once. The condition is checked before every iteration. The afterthought runs after each completed iteration.
Use for loops when you need an index, a fixed number of repeats, a custom step size or precise control over when the loop stops.
Modern JavaScript often uses array methods or for...of for simple collection work. The classic for loop still matters because it makes the counter and stopping rule explicit.
Creates the counter, usually let index = 0.
Keeps the loop running while it is true.
Updates the counter after each iteration.
items[index] reads the current array item.
Examples
const labels = ["Start", "Build", "Review"];
for (let index = 0; index < labels.length; index += 1) {
console.log(`${index + 1}. ${labels[index]}`);
}
for (let index = 0; index <= labels.length; index += 1) {
console.log(labels[index]);
}
// The final item is undefined because arrays are zero-based.
Rules that matter
Control flow decides which path code takes. Keep that path visible: name conditions, protect loop boundaries, avoid accidental fallthrough and use early exits only when they make the main path clearer.
The counter changes, so let communicates reassignment.
Use index < array.length, not index <= array.length.
Mutating the array while looping over it can create surprises.
If the body grows large, extract a function.
If you do not need the index, for...of is usually cleaner.
The condition must eventually become false.
Production thinking
Loops turn one instruction into repeated work. That power is essential, but one wrong condition can repeat too much, skip data or never stop.
Generated lists, tabs and messages should preserve semantic HTML and readable order when loops create markup.
Production loops need safe boundaries, predictable data and clear performance expectations for large collections.
If loops generate content or links, wrong indexes can create missing, duplicate or broken output.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Read each question out loud and answer it without looking at the examples. Control flow only clicks when you can trace the path in your own words.