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

JS Prototype Chain

The prototype chain is the way objects look up things they do not have by asking their parent, then the parent's parent, like asking your parents when you do not know an answer. It ends when nothing is left to ask.
Syntax
javascript
object.__proto__
Object.getPrototypeOf(object);
Constructor.prototype.methodName = function() { };

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.

उदाहरण: 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.

उदाहरण: 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.

उदाहरण: 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.

उदाहरण: Overriding Prototype Values

javascript
// Declare the constant `proto`, set to `{ greet() { return "from prototype"; } }`
// Declare the constant `proto`, set to `{ greet() { return "from prototype"; } }`
const proto = { greet() { return "from prototype"; } };
// Declare the constant `obj`, set to `Object.create(proto)`
// Declare the constant `obj`, set to `Object.create(proto)`
const obj = Object.create(proto);
// Assign `() => "from own property"` to `obj.greet`
// Assign `() => "from own property"` to `obj.greet`
obj.greet = () => "from own property";
// Print `obj.greet()` to the console
// Print `obj.greet()` to the console
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.

उदाहरण: Prototype Chain Lookup

javascript
const obj = {};
console.log(Object.getPrototypeOf(Object.getPrototypeOf(obj))); // null, chain ends
console.log(obj.toString()); // inherited from Object.prototype
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

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.