← Back to JavaScript Course | Chapter 7: OOP & Prototypes | Lesson 1 of 6

JS Prototype Chain

What Is the Prototype Chain?

JavaScript objects can inherit properties from other objects. This lookup continues through a prototype chain. This is the mechanism underlying every method call on a built-in type — array methods like .map() aren't stored on each array instance, they live once on Array.prototype.

Example: What Is the Prototype Chain?

javascript
const arr = [1, 2, 3];
console.log(arr.map === Array.prototype.map); // true, shared on the prototype

Checking Prototypes

You can inspect an object's prototype with Object.getPrototypeOf. It returns the object used for inheritance. This is useful for confirming inheritance relationships at runtime, such as verifying an object was actually created via a particular constructor's prototype.

Example: Checking Prototypes

javascript
const arr = [];
console.log(Object.getPrototypeOf(arr) === Array.prototype);

Inherited Properties

A property may belong to the object itself or come from its prototype. The in operator checks both places. Own properties always take precedence in this lookup, which is why you can override an inherited default by simply assigning a same-named property directly on the object.

Example: Inherited Properties

javascript
const obj = { greet() {} };
console.log("greet" in obj);       // true, own property
console.log("toString" in obj);    // true, inherited from prototype

Overriding Prototype Values

An object can define its own property with the same name as a prototype property. The own property is used first. This lets a specific instance customize behavior — like a special-case object overriding a normally-shared method — without altering the shared prototype for every other instance.

Example: Overriding Prototype Values

javascript
const proto = { greet() { return "from prototype"; } };
const obj = Object.create(proto);
obj.greet = () => "from own property";
console.log(obj.greet());

Prototype Chain Lookup

JavaScript checks the object first, then its prototype, and keeps going until it reaches null. Every prototype chain eventually terminates at Object.prototype and then null, which is why plain objects still inherit common methods like toString() by default.

Example: Prototype Chain Lookup

javascript
const obj = {};
console.log(Object.getPrototypeOf(Object.getPrototypeOf(obj))); // null, chain ends
console.log(obj.toString()); // inherited from Object.prototype
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.