JavaScript Interview Question #105

What is the prototype chain?

Closures, Prototypes, Classes & Advanced Functions Mid-Level Intermediate

Quick Interview Answer

The prototype chain is the sequence of prototype links JavaScript follows when resolving a property. It starts with the object itself, then continues through its prototype, that prototype's prototype, and so on until a matching property is found or the chain reaches null.

Detailed Explanation

The prototype chain is the sequence of prototype links JavaScript follows when resolving a property. It starts with the object itself, then continues through its prototype, that prototype's prototype, and so on until a matching property is found or the chain reaches null.

Understanding this lookup process explains inherited methods such as toString and how class inheritance works internally in JavaScript.

Code Example

const parent = {
    role: "User"
};

const child = Object.create(parent);
child.name = "Asif";

console.log(child.name); // own property
console.log(child.role); // inherited from parent
console.log(Object.getPrototypeOf(child) === parent); // true