Course contentsShow
← JavaScript
Lesson 357 of 2,72310. LoopsFree lesson

Inherited Properties in For...in

Learn how for...in walks the prototype chain and includes inherited properties.

Inherited Properties in For...in

You'll learn how for...in walks up the prototype chain and includes inherited properties, not just an object's own.

The Prototype Chain Connection

Unlike for...of which iterates values, for...in loops through an object's enumerable property names—including those inherited from its prototype chain. This means you'll see properties defined on the object itself and properties it inherits from parent objects.

Think of it like searching your house for items: you find things in your own room, but you also find things in shared family spaces (the prototype).

How It Works

When you use for...in on an object, JavaScript:

  1. Lists the object's own enumerable properties
  2. Walks up to the prototype
  3. Lists inherited enumerable properties
  4. Continues until it reaches null
const parent = { inherited: 'from parent' };
const child = Object.create(parent);
child.own = 'my property';

for (let key in child) {
  console.log(key);
}
// Logs: 'own', 'inherited'

Both properties appear in the loop, even though inherited isn't directly on child.

Filtering to Own Properties

To check if a property belongs directly to the object (not inherited), you'd need to filter manually—but we haven't covered that technique yet. Just know that for...in sees everything enumerable in the chain.

Key Takeaway: for...in includes inherited enumerable properties from the prototype chain, not just the object's own properties—making it broader than you might expect.