JavaScript is a prototype-based language.
Every object in JavaScript has an internal property called [[Prototype]], which is simply a reference to another object.
This “other object” contains shared properties and methods, and JavaScript uses it for inheritance.
✅ 1. Prototype = Parent Object
When you access a property on an object, JS checks:
- Does the object itself have the property?
- If not, check its prototype
- If still not found, check the prototype’s prototype → and so on
- Until it reaches
null
This chain is called the Prototype Chain.
🧪 Example: Prototype Inheritance
const hero = {
fly() {
console.log("Flying...");
}
};
const ironman = Object.create(hero);
ironman.name = "Tony Stark";
ironman.fly(); // Flying... (inherited)
console.log(ironman.name); // Tony Stark
ironman inherits fly() from hero through its prototype.
🔥 2. Functions Also Have Prototype
A JavaScript function has a special property:
function Foo() {}
Foo.prototype
This prototype becomes the prototype of objects created using new:
function Person(name) {
this.name = name;
}
Person.prototype.sayHi = function () {
console.log("Hi " + this.name);
};
const p = new Person("Teekam");
p.sayHi(); // Hi Teekam
✔ All instances share the same sayHi(), reducing memory usage.
⭐ 3. Prototype Chain Visualization
p --> Person.prototype --> Object.prototype --> null
This chain is used to find methods and properties.
✅ 4. Why Do We Need Prototypes?
✔ Shared Methods
All instances can share functions (memory efficient)
✔ Inheritance
Objects can inherit behavior from other objects
✔ Core JS functions use prototypes
Like arrays:
[].__proto__ === Array.prototype // true
🔍 5. Example of Prototype Chain Lookup
const obj = {};
When you write:
obj.toString();
JS looks like this:
obj → Object.prototype → toString()
🎯 Short and Crisp Interview Answer
Prototype in JavaScript is a mechanism that allows objects to inherit properties and methods from other objects.
Every object has a prototype, and the lookup happens through the prototype chain.
Functions have a prototype object whose properties are shared by all instances created usingnew.