AlgoInsight

Q&A

What is a JavaScript prototype?

By AlgoInsight Team2026-06-07arraysoopjavascript

Overview

JavaScript uses prototypal inheritance rather than classical class-based inheritance (even ES6 class is syntactic sugar over prototypes). Objects inherit directly from other objects through a chain of prototype links. Understanding the prototype chain explains how method lookup works, why all instances can share one copy of a method, and how class/new operate under the hood.

graph TD
    A[myArray instance] -->|__proto__| B[Array.prototype]
    B -->|__proto__| C[Object.prototype]
    C -->|__proto__| D[null]
    A -.lookup 'push'.-> B
    style B fill:#1e1b4b,color:#fff

The Prototype Chain

When you read obj.prop, JS checks obj itself first; if absent, it follows obj's prototype link, then that object's prototype, and so on until found or until the chain ends at null:

const arr = [1, 2, 3];
arr.push(4);   // 'push' isn't on arr; found on Array.prototype
arr.toString();// not on arr or Array.prototype; found on Object.prototype

This is why thousands of arrays don't each carry their own push - they all delegate to the single Array.prototype.push.

Accessing an Object's Prototype

const obj = {};
Object.getPrototypeOf(obj);     // Object.prototype  (the correct way)
obj.__proto__;                  // same, but __proto__ is legacy/discouraged
Object.getPrototypeOf(obj) === Object.prototype;  // true

Use Object.getPrototypeOf() / Object.setPrototypeOf() rather than the legacy __proto__.

A Function's prototype Property

This is the common point of confusion. Functions have a prototype property - an object that becomes the prototype of instances created with new:

function Animal(name) {
  this.name = name;
}
Animal.prototype.speak = function () {      // shared by all instances
  return `${this.name} makes a sound`;
};

const dog = new Animal("Rex");
dog.speak();                                 // "Rex makes a sound"
Object.getPrototypeOf(dog) === Animal.prototype;  // true

new Animal(...) creates an object whose prototype is Animal.prototype, so dog inherits speak. Defining methods on the prototype means one shared copy instead of one per instance.

Prototypal Inheritance Between Objects

const base = { greet() { return "hi"; } };
const child = Object.create(base);   // child's prototype IS base
child.greet();                        // "hi" - inherited
Object.getPrototypeOf(child) === base;// true

Object.create(proto) makes a new object with proto as its prototype - direct object-to-object inheritance.

ES6 class Is Sugar Over Prototypes

class Animal {
  constructor(name) { this.name = name; }
  speak() { return `${this.name} makes a sound`; }
}
// Under the hood: methods live on Animal.prototype, exactly like the function example.
Object.getPrototypeOf(new Animal("Rex")) === Animal.prototype;  // true

class is more readable but the underlying mechanism is still the prototype chain.

prototype vs proto (the key distinction)

So dog.__proto__ === Animal.prototype. Mixing these up is the classic mistake.

Why This Matters in Interviews

The prototype chain is one of the most important and most-tested JS concepts. Be able to explain: property lookup walks the chain to null; methods are shared via the prototype (memory efficiency); the difference between a function's prototype property and an object's __proto__/[[Prototype]] link; Object.create for object-to-object inheritance; and that ES6 class is sugar over all this. Using Object.getPrototypeOf over __proto__ shows current best practice.

Common Pitfalls

References

More answers