# The Prototype Chain

> Discover JavaScript's hidden inheritance system. Learn how objects magically share methods by searching up an invisible chain of prototypes.

Subject: JavaScript | Lesson 22 of 63 | 8 min read
Interactive lesson: https://codewithanimation.in/javascript/prototype-chain
Previous: [Object Methods & Property Descriptors](https://codewithanimation.in/javascript/object-methods-and-descriptors.md) | Next: [Classes: Syntax & Inheritance](https://codewithanimation.in/javascript/classes.md)

## What is a Prototype?

In JavaScript, almost every object has a hidden internal property called `[[Prototype]]` (historically accessed via `__proto__`).

This property is simply a reference (a pointer) to another object. That other object is called its 'prototype', and it acts as a fallback or parent.

## The Property Lookup Algorithm

When you try to read a property on an object (like `myArray.push()`), JavaScript first checks the object itself.

If it doesn't find the property there, it doesn't immediately give up. Instead, it follows the `__proto__` link to the prototype object and looks there.

If it still doesn't find it, it checks *that* object's prototype, walking up the 'Prototype Chain' until it either finds the property or hits `null`.

## Where do array methods come from?

When you create an array `const arr = []`, it is born with a `__proto__` pointing to `Array.prototype`.

`Array.prototype` is a massive object that holds all the standard array methods: `.push()`, `.map()`, `.filter()`, etc.

Because of the prototype chain, your tiny empty array instantly gains access to all those methods without having to copy them into its own memory!

## Common mistake: Modifying built-in prototypes

Because prototypes are just regular objects, you can actually mutate them (e.g., `Array.prototype.myCustomMethod = ...`). **Never do this in production.** It is called 'monkey patching' and can cause massive conflicts with other libraries or future JavaScript updates.

## Tip: The end of the chain

Almost all prototype chains eventually end at `Object.prototype`. And the prototype of `Object.prototype` is `null`. That is where the search stops and JavaScript finally returns `undefined`.

## Try it: The Fallback Mechanism

Trace the path JavaScript takes when you call `.toString()` on a simple array, watching it walk up the invisible prototype chain.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/prototype-chain

## Check yourself

### Question 1

What does JavaScript do if you try to read a property that doesn't exist on an object?

- A. It immediately throws a ReferenceError.
- B. It checks the object's prototype (__proto__) to see if it exists there.
- C. It automatically creates the property and sets it to null.

Correct answer: B

If a property is missing, JavaScript treats the prototype object as a fallback and searches it instead.

- Why not A: Incorrect. JavaScript is more forgiving and tries to find a fallback.
- Why not C: Incorrect. JavaScript does not mutate the object during a read operation.

### Question 2

Why don't we have to copy the code for `.push()` into every single array we create?

- A. Because the JavaScript engine compiles it into machine code.
- B. Because every array delegates those calls to the shared Array.prototype object.
- C. Because .push() is a global function, not an array method.

Correct answer: B

Prototypal inheritance is highly memory efficient. Millions of arrays can all share a single copy of the `.push()` method by delegating up the prototype chain.

- Why not A: Incorrect. While true, that isn't how object inheritance works.
- Why not C: Incorrect. `.push()` lives specifically on arrays.

### Question 3

What is at the very top of almost every prototype chain?

- A. Object.prototype
- B. Window
- C. Function.prototype

Correct answer: A

`Object.prototype` is the grand-parent of nearly all objects in JavaScript. Its own prototype is `null`, which safely ends the chain.

- Why not B: Incorrect. `Window` is the global object in browsers, not the top of the prototype chain.
- Why not C: Incorrect. Functions inherit from `Function.prototype`, but `Function.prototype` itself inherits from `Object.prototype`.

## Remember this

- JavaScript uses Prototypal Inheritance, not classical class-based inheritance.
- Every object has a hidden `__proto__` property pointing to a fallback object.
- Property lookups walk up this chain until the property is found or `null` is reached.
- Methods like `.push()` and `.map()` live on `Array.prototype`, not on the individual arrays.
- The chain usually terminates at `Object.prototype`, which points to `null`.
