JavaScript For In
The for…in loop iterates over the property names (keys) of an object. It's the classic way to visit every property when you don't know in advance which properties an object has.
Syntax
for (const key in object) {
// key is a property name (always a string)
}const car = { brand: "Tesla", model: "Model 3", year: 2023 };
for (const key in car) {
console.log(key, "=", car[key]);
}brand = Tesla
model = Model 3
year = 2023
Note that you must use bracket notation car[key] to read the value — car.key would look for a property literally called "key".
Practical example: summing values
const expenses = { rent: 950, food: 320, transport: 85, fun: 120 };
let total = 0;
let biggest = "";
for (const category in expenses) {
total += expenses[category];
if (biggest === "" || expenses[category] > expenses[biggest]) {
biggest = category;
}
}
console.log("Total:", total);
console.log("Biggest expense:", biggest);Total: 1475
Biggest expense: rent
Inherited properties
for…in also visits inherited, enumerable properties from an object's prototype chain. Plain object literals don't inherit any enumerable properties, but objects created from a prototype can:
const base = { kind: "animal" };
const dog = Object.create(base); // dog inherits from base
dog.name = "Rex";
dog.sound = "woof";
for (const key in dog) {
console.log(key, Object.hasOwn(dog, key) ? "(own)" : "(inherited)");
}
console.log(Object.keys(dog)); // own keys onlyname (own)
sound (own)
kind (inherited)
[ 'name', 'sound' ]
Filter with Object.hasOwn(obj, key) if you only want the object's own properties — or skip for…in entirely and loop over Object.keys(obj) / Object.entries(obj) with for…of.
Don't use for…in on arrays
for…in works on arrays, but it gives you the indexes as strings, also visits any extra properties added to the array, and isn't meant for ordered data:
const colors = ["red", "green"];
colors.note = "extra property";
for (const i in colors) {
console.log(i, typeof i, colors[i]);
}
for (const color of colors) { // for…of: just the elements
console.log(color);
}0 string red
1 string green
note string extra property
red
green
Tip: Rule of thumb —
for…infor object keys,for…offor array values (and other iterables like strings, Maps and Sets).
Property order
Keys are visited in a defined order: first integer-like keys in ascending numeric order, then string keys in the order they were added:
const mixed = { b: 1, 2: "two", a: 2, 1: "one" };
for (const k in mixed) console.log(k);1
2
b
a
for…in vs the alternatives
| Loop | Iterates over | Includes inherited? |
|---|---|---|
for (const k in obj) |
enumerable keys | ✅ yes |
for (const k of Object.keys(obj)) |
own enumerable keys | ❌ no |
for (const [k, v] of Object.entries(obj)) |
own key/value pairs | ❌ no |
for (const v of array) |
array values | — |
Test yourself
What does for (const k in obj) give you in k?
- Each value
- Each property name
- Each [key, value] pair
- The index as a number
How do you read the value inside for (const k in user)?
- user.k
- user[k]
- k.value
- user(k)
Which loop is recommended for the values of an array?
- for…in
- for…of
- for…each
- while…in