The prototype chain in JavaScript is a mechanism through which objects inherit properties and methods from other objects.
It’s part of JavaScript’s prototype-based inheritance model.
1️⃣ Every Object Has a Prototype
When you create an object, JavaScript automatically links it to another object called its prototype, from which it can inherit properties or methods.
const person = { greet() { console.log("Hello!"); } };
const user = Object.create(person);
user.greet(); // "Hello!"
Here, user doesn’t have greet(), so JS looks up its prototype (person) — this lookup process forms the prototype chain.
2️⃣ The Chain Continues Upward
Every object’s prototype itself has a prototype — until it reaches the end of the chain (null).
console.log(Object.getPrototypeOf(user) === person); // true
console.log(Object.getPrototypeOf(person) === Object.prototype); // true
console.log(Object.getPrototypeOf(Object.prototype)); // null (end)
Prototype lookup order: user → person → Object.prototype → null
3️⃣ Functions Have prototype Property
When you define a function, it automatically gets a prototype property that is used when creating objects via new.
function Animal() {}
Animal.prototype.speak = function () {
console.log("Animal sound");
};
const dog = new Animal();
dog.speak(); // "Animal sound"
Here, dog inherits speak() from Animal.prototype.
4️⃣ Prototype Chain Lookup
When accessing a property:
- JS checks the object itself.
- If not found, it looks up the prototype chain.
- Stops when it reaches
null.
Visualization
dog --> Animal.prototype --> Object.prototype --> null
💡 In Short:
The prototype chain is how JavaScript objects inherit properties and methods from other objects.
It continues upward through prototypes until it reachesObject.prototype, whose prototype isnull.