for in vs for of — the difference, and why for in hands you strings instead of numbers

Here's a list of fruit and a loop that looks perfectly logical:
const fruits = ["apple", "pear"]
for (const fruit in fruits) {
console.log(fruit)
}
You expect "apple" and "pear". You get "0" and "1". In quotes, too — those are strings, not numbers.
One word in the loop — in instead of of — and you're looping over something else entirely. People coming from Python land here especially often: there, for x in list gives you the values. In JavaScript it doesn't. Let's sort out who does what.
In short: numbers or contents
Picture a row of lockers. Each door has a numbered tag; inside there's an item.
- for in walks the tags. It gives you keys: indexes for an array, field names for an object.
- for of walks the contents. It gives you the values themselves: "apple", "pear".
English helps you remember. In — "what's in this thing" (which keys). Of — "the values of what" (what's stored inside). If you get mixed up, just remember: of gives you what you usually want.
The loop itself works the same way: it repeats the body for each element. The only difference is what lands in the variable.
for of — for lists and strings
const fruits = ["apple", "pear"]
for (const fruit of fruits) {
console.log(fruit) // "apple", then "pear"
}
Now that's what you expected. for of works with anything that can be walked in order:
- arrays — gives you the elements, more on arrays;
- strings — gives you letters one by one;
- Map and Set — special collections.
And if you need both the index and the value, there's a trick:
for (const [i, fruit] of fruits.entries()) {
console.log(i, fruit) // 0 "apple", then 1 "pear"
}
Notice: here i is a real number, not a string.
Where for of doesn't work. On a plain object:
const user = { name: "Anya", age: 12 }
for (const x of user) {} // TypeError: user is not iterable
"Not iterable" means "can't be walked in order". An object isn't a queue — it's a card with fields.
for in — for an object's fields
This is where for in belongs:
const user = { name: "Anya", age: 12 }
for (const key in user) {
console.log(key, user[key]) // "name" "Anya", then "age" 12
}
It gives you the field names, and you fetch the value yourself with user[key]. How objects work is in what is an object.
Why does it give "0" and "1" as strings for an array? Because to JavaScript an array is also an object, with fields named "0", "1", "2". And an object's field names are always strings. Hence the trap: "0" + 1 gives "01", not 1.
for in has a second annoyance too. It can pick up extra fields — ones the object inherited from a "parent". That's rare in everyday code, but it's exactly why many people advise avoiding it.
The modern way for objects
Today objects are more often looped like this:
for (const [key, value] of Object.entries(user)) {
console.log(key, value) // "name" "Anya", then "age" 12
}
Object.entries turns an object into a list of key–value pairs. And a list can be walked with for of. You get both the field name and the value at once, with no stray fields from "parents".
Which one fits whom
Let's compare on what matters to a beginner:
- What it gives.
for of— values.for in— keys, always as strings. - Arrays.
for of— yes, that's its job.for in— technically works, but gives string indexes. Don't use it. - Strings.
for of— gives letters.for in— string indexes again. - Objects.
for of— crashes with "is not iterable".for in— works, butObject.entriesis better. - Surprise risk. Near zero for
for of. Forfor in— strings instead of numbers, plus inherited fields.
The verdict, no fence-sitting: write for of by default. For an object — for of with Object.entries. Leave for in for other people's code that you need to read.
And if you don't just want to walk a list but build a new list from it, look at .map(). When to pick which is covered in loop vs map.
Why does AI sometimes write for in over an array?
Models learned from old code where it showed up. If you spot it in your project, ask AI to switch to for of and check that no index is being added to a string somewhere.
Does break work in for of?
Yes. break stops the loop, continue skips to the next element. forEach doesn't have them — one more point for for of.
Does Python have for of?
No, only for … in. But in Python it behaves like JavaScript's for of — it gives you values.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





