for...of
Readable value loop with break and continue support.
Iteration means visiting array items one by one. JavaScript gives you loops and methods for different kinds of iteration.
const topics = ["HTML", "CSS", "JavaScript"];
for (const topic of topics) {
console.log(topic);
}
Arrays & Collections
for...of is a readable loop for array values. It works well when you need break, continue or await inside a loop.
forEach runs a callback for each item. It is useful for side effects, but it does not return a transformed array.
Array methods such as map, filter and reduce are also forms of iteration. They are best when the method name matches the result you want.
Readable value loop with break and continue support.
Runs side effects for every item.
Provides index and value pairs.
Useful when manual index control is needed.
Examples
for (const record of records) {
if (record.status === "ready") {
selected = record;
break;
}
}
records.forEach((record) => {
if (record.status === "ready") {
return record;
}
});
// return only exits the callback, not the outer function.
Code patterns
These are the patterns developers search for constantly: reading items, copying arrays, choosing map/filter/reduce, sorting with a comparator, destructuring and using Set or Map.
Loop over values directly.
const topics = ["HTML", "CSS", "JavaScript"];
for (const topic of topics) {
console.log(topic);
}
Read index and value together.
const topics = ["HTML", "CSS"];
for (const [index, topic] of topics.entries()) {
console.log(index, topic);
}
Use forEach when you perform an action for each item.
const topics = ["HTML", "CSS"];
topics.forEach((topic) => {
console.log(topic);
});
Use for...of when you need early exit.
for (const topic of topics) {
if (topic === "CSS") break;
console.log(topic);
}
Rules that matter
Arrays and collections are not just storage. They describe whether the code is selecting, transforming, grouping, counting, sorting or enforcing uniqueness.
It is excellent for values and early exit.
Rendering, logging or collecting can fit forEach.
Use map, filter or reduce for returned arrays or values.
Avoid manual counters when entries communicates the intent.
Manual index control is still valid for some algorithms.
Large loop bodies hide the actual iteration logic.
Production thinking
Iteration is where data starts moving. Good loop choice makes the direction of that movement visible.
Rendering loops should create predictable ordered content and avoid duplicate IDs or missing labels.
Production loops should be readable, avoid unnecessary work and choose early exit when performance matters.
Lists rendered from arrays should keep stable order and reliable text content.
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
A collection method should tell the reader what kind of operation is happening before they read the callback body.