TExcelTAD
Home › JavaScript › JS For In

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

js
for (const key in object) {
  // key is a property name (always a string)
}
js · try it yourself
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

js · try it yourself
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:

js · try it yourself
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 only
name (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:

js · try it yourself
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…in for object keys, for…of for 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:

js · try it yourself
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?

  1. Each value
  2. Each property name
  3. Each [key, value] pair
  4. The index as a number

How do you read the value inside for (const k in user)?

  1. user.k
  2. user[k]
  3. k.value
  4. user(k)

Which loop is recommended for the values of an array?

  1. for…in
  2. for…of
  3. for…each
  4. while…in