prototype
Belongs to constructor functions and contains methods shared by objects created with new.
Understand how JavaScript finds properties and methods through the prototype chain.
Learn how objects inherit from other objects, how property lookup works, why everything ultimately inherits from Object.prototype, and how JavaScript uses prototypes instead of traditional class inheritance.
Estimated Time
15 minutes
Difficulty
Intermediate
Progress
0% Complete
The Prototype Chain is JavaScript's inheritance mechanism. It lets an object inherit properties and methods from another object through a hidden prototype link.
Unlike Java or C#, JavaScript uses prototype-based inheritance. The modern class syntax is still built on top of prototypes.
What the Prototype Chain is
How JavaScript searches for properties
Why objects can use methods they do not own
How arrays, strings, and functions get built-in methods
Why Object.prototype and null matter
How prototype-based inheritance differs from Java or C# classes
How to explain prototypes in interviews
Common prototype mistakes
When JavaScript cannot find something on the current object, it asks the parent prototype, then the next one, until it reaches null.
Family Tree Analogy
Grandfather
^
Father
^
Son
Grandfather owns a house.
Father owns a car.
Son owns a laptop.
If the son needs something:
1. Does the son have it?
2. If not, check the father.
3. If not, check the grandfather.
4. If nobody has it, return undefined.Current object
JavaScript checks here first.
Prototype
Checked only if the property is missing.
Next prototype
JavaScript keeps walking upward.
Not found
Returned when the chain ends without a match.
1const animal = {2eat() {3console.log("Eating...");4}5};67const dog = {8bark() {9console.log("Woof!");10}11};1213Object.setPrototypeOf(dog, animal);1415dog.bark();16dog.eat();
Output
Woof!
Eating...
dog owns bark(), but it does not own eat(). JavaScript follows dog's prototype link to animal and finds eat() there.
1const person = {2country: "Pakistan"3};45const employee = {6name: "Zubair"7};89Object.setPrototypeOf(employee, person);1011console.log(employee.name);12console.log(employee.country);
Output
Zubair
Pakistan
name is found directly on employee. country is not found on employee, so JavaScript checks person and returns Pakistan.
1const grandParent = {2city: "Lahore"3};45const parent = {6car: "Honda"7};89const child = {10laptop: "MacBook"11};1213Object.setPrototypeOf(parent, grandParent);14Object.setPrototypeOf(child, parent);1516console.log(child.city);
Output
Lahore
city is not on child or parent. JavaScript keeps walking upward and finds city on grandParent.
This is the shape to remember for interviews and debugging.
Prototype Chain
child Object
|
v
Parent Prototype
|
v
Grandparent Prototype
|
v
Object.prototype
|
v
nullSuppose we access:
1child.city;
Imagine a developer object linked to a manager object, and the manager linked to a CEO object. If you ask for developer.company, JavaScript checks Developer, then Manager, then CEO, and returns the company when it finds it.
CEO
company: Systems Limited
Manager
department: Commerce
Developer
name: Zubair
Lookup stops as soon as JavaScript finds the property.
person has its own name property, so lookup stops immediately.
Step through source code, current object, prototype, chain, memory, and console output.
1const animal = {2eat() {3return "Eating";4}5};67const dog = Object.create(animal);8dog.name = "Tom";910console.log(dog.eat());
Console
(empty)The animal object owns the shared eat method.
Animate each link in the chain and see where lookup travels.
Constructor functions and ES6 classes both use prototypes for shared methods.
1function Person(name) {2this.name = name;3}45Person.prototype.sayHello = function() {6return "Hello";7};89const user = new Person("Zubair");10user.sayHello();
1class Person {2constructor(name) {3this.name = name;4}56sayHello() {7return "Hello";8}9}1011const user = new Person("Zubair");
Classes are syntactic sugar over prototypes. Methods still live on the class prototype.
Keep these roles separate and prototype questions become much easier.
Belongs to constructor functions and contains methods shared by objects created with new.
Belongs to objects and points to the next object in the prototype chain.
A reference commonly available through the prototype back to the constructor function.
Prototype methods are referenced through links instead of being copied into every object.
inherits methods upward through Dog and Animal
Built-in methods like push(), toUpperCase(), and call() are found through the same prototype lookup process.
1const numbers = [10, 20, 30];23numbers.push(40);
Where does push() come from?
It comes from Array.prototype. The array instance does not store a separate copy of push().
Array chain
numbers | v Array.prototype | v Object.prototype | v null
1const name = "Zubair";23console.log(name.toUpperCase());
Where does toUpperCase() come from?
JavaScript temporarily wraps the primitive string and looks up toUpperCase() on String.prototype.
String chain
name | v String.prototype | v Object.prototype | v null
1function greet() {}23greet.call(null);
Where does call() come from?
Functions are objects too. call() is found through Function.prototype.
Function chain
greet | v Function.prototype | v Object.prototype | v null
Universal ending
Object | v Object.prototype | v null
If JavaScript reaches null, it stops searching. At that point, a missing property returns undefined.
This concept explains inheritance, shared methods, and many built-in JavaScript APIs.
Code reuse
Inheritance through object links
Shared methods instead of duplicate methods on every object
Efficient memory usage
Built-in methods like map(), filter(), push(), toUpperCase(), and call()
The Prototype Chain is JavaScript's inheritance mechanism. Every object has an internal reference to another object called its prototype. When a property or method is accessed, JavaScript first looks for it on the object itself. If it is not found, JavaScript follows the prototype chain, checking each prototype until it either finds the property or reaches null. This allows objects to inherit shared properties and methods efficiently without duplicating them.
Run, reset, copy, and step through prototype lookup examples with memory notes.
1const animal = {2eat() {3return "Eating";4}5};67const dog = Object.create(animal);8dog.eat();
Click Run to see output.
Object.create() creates a new object with the parent object as its prototype.
Prototypes power many objects you use every day.
Uses internal prototype links for shared behavior, lookup, or object APIs.
Uses internal prototype links for shared behavior, lookup, or object APIs.
Uses internal prototype links for shared behavior, lookup, or object APIs.
Uses internal prototype links for shared behavior, lookup, or object APIs.
Uses internal prototype links for shared behavior, lookup, or object APIs.
Uses internal prototype links for shared behavior, lookup, or object APIs.
Uses internal prototype links for shared behavior, lookup, or object APIs.
Uses internal prototype links for shared behavior, lookup, or object APIs.
Lookup stops when the property is found, or when the chain reaches null.
These mistakes make prototype questions harder than they need to be.
Thinking prototype copies methods.
Confusing prototype with __proto__.
Thinking classes replace prototypes.
Editing Object.prototype.
Breaking inheritance by overwriting prototype incorrectly.
Each answer includes a simple explanation, technical explanation, interview answer, example, memory trick, and common mistake.
Simple explanation
A prototype is a hidden link from one object to another object.
Technical explanation
Each object has an internal [[Prototype]] reference used for property lookup.
Interview answer
A prototype is the object JavaScript checks when a property is not found on the current object.
Real-world example
dog can use animal.eat through its prototype link.
Memory trick
Prototype is the backup place to search.
Common mistake
Thinking prototype means properties are copied.
Fifteen questions with explanations, wrong-answer reasoning, memory tricks, and interview tips.
Progress
0/15 answered
Quick revision for the terms that usually get confused.
Prefer ES6 classes for readability.
Understand that classes still use prototypes.
Avoid modifying Object.prototype.
Use Object.create() intentionally.
Keep shared methods on prototypes.