Skip to content
Objects In DepthChapter 28 / 47Advanced4 min read

Prototypes & Inheritance

Learn what JavaScript prototype inheritance is, how the prototype chain works, and how objects share behavior with Object.create.

On this page

1. What are prototypes?

In JavaScript, every object has a hidden link called [[Prototype]]. When you look for a property on an object and it is not found there, JavaScript follows that link and looks in the prototype object.

This system is called prototypal inheritance. JavaScript classes also run on top of it under the hood.

2. Why does it matter?

If you have 1000 user objects and each has its own copy of a greet function, memory is wasted. With a prototype, all objects share a single method.

Without a prototypeWith a prototype
A separate copy of the method in every objectThe method lives in one place, all objects share it
More memory usedLess memory used
To change a method, update every objectOne change on the prototype affects everything
Code is repeatedReuse and inheritance are easy

3. Where is it used?

PlaceRole of the prototype
Arrays[1, 2].map() actually comes from Array.prototype.map
Strings"hi".toUpperCase() comes from String.prototype
Classesclass methods are stored on the prototype
LibrariesPutting shared behavior on a base object and extending it
InterviewsThe prototype chain and __proto__ vs prototype are common questions

4. How does it work?

Property lookup works like a chain. JavaScript goes from the bottom up until it finds the property or the chain ends at null.

Prototype chain lookup
Look for the property on the user object
Not found, so look in userPrototype
Not found, so look in Object.prototype
Not found, so the chain ends at null
The result is undefined

__proto__ and prototype are different things:

ThingMeaning
obj.__proto__The old way to see an object's prototype, today use Object.getPrototypeOf(obj)
Fn.prototypeExists only on functions and classes, and it becomes the prototype of objects made with new Fn()

5. Code example

Inheritance with Object.create

js
const animal = {
  speak() {
    return `${this.name} makes a sound`;
  },
};

const dog = Object.create(animal);
dog.name = "Moti";

console.log(dog.speak());
console.log(Object.getPrototypeOf(dog) === animal);
console.log(Object.hasOwn(dog, "speak"));

Constructor function and prototype

js
function User(name) {
  this.name = name;
}

User.prototype.greet = function () {
  return `Hello, ${this.name}`;
};

const ali = new User("Ali");
const sara = new User("Sara");

console.log(ali.greet());
console.log(ali.greet === sara.greet);

How does the code work?

LineWhat is happening
Object.create(animal)A new empty object whose prototype is animal
dog.speak()dog has no speak, so animal.speak is found through the chain
Object.hasOwn(dog, "speak")false, because the method is borrowed, not its own
User.prototype.greetThe method sits once on the prototype, all users share it
ali.greet === sara.greettrue, both use the same function

Interview tip

A common question is: "What is the end of the prototype chain?" The answer: Object.prototype, and its prototype is null. That is why Object.getPrototypeOf(Object.prototype) returns null.

6. Common mistakes

Mistake: Making a prototype method with an arrow function

Why? An arrow function has no this of its own, so this.name comes out undefined.

Better: Use a normal function or class method syntax for prototype methods.

Mistake: Modifying built-in prototypes like Array.prototype

Why? It can conflict with other libraries and with future JavaScript features.

Better: Write your own helper functions separately and do not touch built-in prototypes.

Mistake: Setting __proto__ directly in code

Why? It is an old and slow approach that hurts both performance and readability.

Better: Use Object.create, Object.getPrototypeOf, and classes.

Mistake: Forgetting inherited properties in a for...in loop

Why? for...in also gives the prototype's enumerable properties, which leads to odd results.

Better: Use Object.keys or Object.entries, which return only own properties.

7. What to learn before and after

Suggested order
Objects
this keyword
Prototypes
Classes

Final quick revision

PointDetail
PrototypeThe object that properties are borrowed from
Prototype chainLookup goes upward until it reaches null
Object.create(proto)A new object with the given prototype
Object.getPrototypeOfThe modern way to read a prototype
Object.hasOwnCheck whether a property is own or inherited
ClassesClean syntax on top of prototypes