Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →

JavaScript Prototypes and Inheritance Interview Questions (With Answers)

11 JavaScript prototype and inheritance interview questions with answers and code: the prototype chain, __proto__ vs prototype, constructor functions, what new does, Object.create, ES6 classes, instanceof and own vs inherited properties.

Sumit KumarUpdated Oct 12, 20266 min readJavaScript
JavaScript Prototypes and Inheritance Interview Questions (With Answers)

Even though JavaScript has a class keyword, every object still inherits through prototypes. Interviewers ask about prototypes to see whether you understand what your classes compile down to. Here are 11 questions with answers, diagrams in words and code.

The JavaScript prototype chain: a dog object with its own properties links to Dog.prototype, then Animal.prototype, then Object.prototype and finally null

The prototype chain#

Q1. What is a prototype in JavaScript?#

Answer: Every object has a hidden internal link, [[Prototype]], to another object (or null). When you read a property that the object doesn't have, JavaScript looks it up on the prototype, then on the prototype's prototype, and so on. This series of links is the prototype chain. It lets many objects share methods without copying them.

JavaScript
const animal = { eats: true, walk() { return "walking"; } };
const rabbit = Object.create(animal);   // rabbit's prototype is animal
rabbit.jumps = true;

rabbit.jumps;   // true  (own property)
rabbit.eats;    // true  (found on animal)
rabbit.walk();  // "walking"
rabbit.fly;     // undefined (searched up to Object.prototype, then null)

Q2. What is the difference between __proto__ and prototype?#

obj.__proto__Fn.prototype
Exists onEvery objectFunctions (and classes) only
What it isA getter for the object's actual prototypeThe object that will become the prototype of instances made with new Fn()
Modern wayObject.getPrototypeOf(obj) / Object.setPrototypeOfSame property
JavaScript
function Dog(name) { this.name = name; }
const rex = new Dog("Rex");

Object.getPrototypeOf(rex) === Dog.prototype;   // true
rex.__proto__ === Dog.prototype;                // true (legacy accessor)
Dog.prototype.constructor === Dog;              // true
Object.getPrototypeOf(Dog.prototype) === Object.prototype; // true
One-line answer

prototype is the blueprint a constructor hands to the objects it creates; __proto__ is the link each object actually holds.

Q3. Where does the chain end, and what does hasOwnProperty tell you?#

Answer: Normally at Object.prototype, whose prototype is null. Object.hasOwn(obj, key) (or obj.hasOwnProperty(key)) checks only the object itself; the in operator checks the whole chain.

JavaScript
const rabbit = Object.create({ eats: true });
rabbit.jumps = true;

"eats" in rabbit;                 // true  (inherited)
Object.hasOwn(rabbit, "eats");    // false
Object.hasOwn(rabbit, "jumps");   // true

for (const key in rabbit) console.log(key);  // jumps, eats (for...in includes inherited enumerables)
Object.keys(rabbit);                         // ["jumps"] (own only)

const dict = Object.create(null); // no prototype at all: safe for maps of user keys
dict.toString;                    // undefined

Constructors and new#

Q4. What happens when you call a function with new?#

  1. A new empty object is created.
  2. Its prototype is set to Fn.prototype.
  3. Fn runs with this bound to the new object.
  4. If Fn returns an object, that object is returned; otherwise the new object is.
JavaScript
function myNew(Fn, ...args) {
  const obj = Object.create(Fn.prototype);   // steps 1 and 2
  const result = Fn.apply(obj, args);        // step 3
  return result !== null && (typeof result === "object" || typeof result === "function")
    ? result
    : obj;                                   // step 4
}

function User(name) { this.name = name; }
User.prototype.hi = function () { return `Hi ${this.name}`; };

myNew(User, "Asha").hi();   // "Hi Asha"

Q5. Why put methods on the prototype instead of inside the constructor?#

JavaScript
function Slow(name) {
  this.name = name;
  this.greet = function () { return this.name; };  // new function per instance
}

function Fast(name) { this.name = name; }
Fast.prototype.greet = function () { return this.name; }; // one shared function

new Slow("a").greet === new Slow("b").greet;  // false
new Fast("a").greet === new Fast("b").greet;  // true

Answer: Methods on the prototype are created once and shared by every instance, which saves memory. Per-instance functions are only worth it when each needs its own closure over private data.

Q6. How did inheritance work before ES6 classes?#

JavaScript
function Animal(name) { this.name = name; }
Animal.prototype.eat = function () { return `${this.name} eats`; };

function Dog(name, breed) {
  Animal.call(this, name);                 // 1. inherit instance properties
  this.breed = breed;
}
Dog.prototype = Object.create(Animal.prototype); // 2. inherit methods
Dog.prototype.constructor = Dog;                 // 3. fix the constructor pointer
Dog.prototype.bark = function () { return "Woof"; };

const d = new Dog("Rex", "Lab");
d.eat();               // "Rex eats"
d instanceof Animal;   // true

Interviewers want all three steps; forgetting to reset constructor is a common mistake.

Classes and modern APIs#

Q7. Are ES6 classes real classes?#

Answer: No, they are mostly syntax over prototypes. A class is a special function; methods go on ClassName.prototype; extends sets up the same chain as Q6. There are real differences though:

  • Classes must be called with new (calling them directly throws).
  • Class bodies run in strict mode.
  • Class declarations are hoisted but stay in the temporal dead zone.
  • Methods are non-enumerable.
  • Classes support #private fields, static blocks and super.
JavaScript
class Animal {
  #sound = "...";                         // truly private field
  constructor(name) { this.name = name; }
  speak() { return `${this.name} says ${this.#sound}`; }
  static create(name) { return new this(name); }
}

class Dog extends Animal {
  constructor(name) {
    super(name);                          // must call before using this
  }
  speak() { return super.speak() + " (woof)"; }
}

typeof Dog;                                               // "function"
Object.getPrototypeOf(Dog.prototype) === Animal.prototype; // true
Object.getPrototypeOf(Dog) === Animal;                     // true: statics inherit too

Q8. What does Object.create do, and how is it different from new?#

Answer: Object.create(proto, descriptors?) makes a new object whose prototype is proto, without running any constructor. new Fn() also runs Fn to initialise the object.

JavaScript
const personProto = {
  greet() { return `Hello, ${this.name}`; },
};

const p = Object.create(personProto, {
  name: { value: "Ravi", writable: true, enumerable: true },
});
p.greet();   // "Hello, Ravi"

Q9. How does instanceof work? Implement it.#

Answer: obj instanceof Fn walks up obj's prototype chain and returns true if it finds Fn.prototype.

JavaScript
function myInstanceOf(obj, Fn) {
  if (obj === null || (typeof obj !== "object" && typeof obj !== "function")) return false;
  let proto = Object.getPrototypeOf(obj);
  while (proto !== null) {
    if (proto === Fn.prototype) return true;
    proto = Object.getPrototypeOf(proto);
  }
  return false;
}

myInstanceOf([], Array);    // true
myInstanceOf([], Object);   // true
myInstanceOf({}, Array);    // false

Follow-up: instanceof fails across iframes or realms (each has its own Array), which is why Array.isArray exists.

Gotchas#

Q10. What is "shadowing", and what happens when you write to an inherited property?#

JavaScript
const proto = { count: 0, tags: [] };
const a = Object.create(proto);
const b = Object.create(proto);

a.count = 5;        // creates an OWN property on a (shadows proto.count)
b.count;            // 0

a.tags.push("x");   // mutates the SHARED array on proto!
b.tags;             // ["x"]

Answer: Assigning a property always creates or updates it on the object itself, hiding (shadowing) the inherited one. But mutating an inherited object or array changes the shared value for every instance. Keep mutable state in the constructor, not on the prototype.

Q11. Should you extend built-in prototypes like Array.prototype?#

Answer: Not in application or library code. Adding Array.prototype.last can clash with future standard methods (this famously happened with Array.prototype.flatten, which had to be renamed flat), breaks for...in loops if made enumerable, and affects every other script on the page. The acceptable exception is a spec-compliant polyfill guarded by a feature check.

JavaScript
// Acceptable: polyfill a standard method only if missing
if (!Array.prototype.at) {
  Object.defineProperty(Array.prototype, "at", {
    value(i) { i = Math.trunc(i) || 0; return this[i < 0 ? this.length + i : i]; },
    writable: true, configurable: true, enumerable: false,
  });
}

Quick revision#

QuestionShort answer
Where are class methods stored?On Class.prototype
Get an object's prototypeObject.getPrototypeOf(obj)
Object with no prototypeObject.create(null)
Own vs inherited checkObject.hasOwn vs in
End of every chainnull
What super() doesRuns the parent constructor to initialise this

Related: this, call, apply and bind interview questions and JavaScript design patterns.

Key takeaways#

  • Every object links to a prototype; property lookups walk this chain until they reach null.
  • Fn.prototype becomes the prototype of objects created with new Fn(); __proto__ is each object's actual link.
  • new creates an object, links it to the prototype, runs the constructor and returns the object.
  • ES6 classes are syntax over prototypes, with extras like strict mode, private fields and mandatory new.
  • Put shared methods on the prototype, keep mutable state per instance, and don't extend built-ins.