for...of
Reads values from iterables such as arrays, strings, sets and maps.
for...of loops over values from an iterable. for...in loops over enumerable property names. They look similar, but they are not the same tool.
const tools = ["HTML", "CSS", "JavaScript"];
for (const tool of tools) {
console.log(tool);
}
const settings = { theme: "dark", density: "compact" };
for (const key in settings) {
console.log(key, settings[key]);
}
Control Flow
for...of works with iterable values such as arrays, strings, maps, sets and many DOM collections. It gives you each value directly.
for...in works with enumerable property keys on an object. It gives you keys, not values. It can also include inherited enumerable properties, so object ownership checks can matter.
For arrays, prefer for...of, array methods or a classic for loop. for...in on arrays loops over keys as strings and can behave unexpectedly with custom properties.
Reads values from iterables such as arrays, strings, sets and maps.
Reads enumerable property keys from objects.
Checks that a property belongs directly to the object.
Pairs keys and values for arrays, maps and objects when used with the right helper.
Examples
for (const item of items) {
console.log(item.title);
}
for (const key in settings) {
if (Object.hasOwn(settings, key)) {
console.log(key, settings[key]);
}
}
const items = ["alpha", "beta"];
for (const item in items) {
console.log(item); // "0", then "1"
}
// The variable is the key, not the value.
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.
It gives you each value directly.
It is about property names, not array values.
Object.hasOwn protects against inherited enumerable properties.
It is often clearer than for...in plus bracket access.
for...of can loop over characters.
Changing the collection during iteration can produce confusing results.
Production thinking
Choosing the wrong loop changes what your variable contains. That creates bugs that look small but spread quickly through rendering and data processing.
Loops often generate lists and controls. Correct iteration keeps labels and item order predictable.
Production code should make data shape obvious. Arrays, maps, sets and plain objects deserve different iteration patterns.
Wrong iteration can output indexes instead of meaningful text or links, which hurts both users and crawlers.
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.