Skip to main content

instanceof and prototypes

the instanceof operator is directly tied to prototypes and literally checks:

"Is the constructor's prototype somewhere in the object's prototype chain?"

Let's break it down step by step.


In short

Syntax:

javascript
obj instanceof Constructor

Returns true if the prototype chain of the object obj contains the object Constructor.prototype.

Otherwise it returns false.


Example

javascript
function User() {} const tim = new User(); console.log(tim instanceof User); // true console.log(tim instanceof Object); // true console.log(tim instanceof Array); // false

Under the hood instanceof checks:

javascript
tim.__proto__ === User.prototype or tim.__proto__.__proto__ === User.prototype and so on until it reaches null

How it works "under the hood"

javascript
function instanceOf(obj, Constructor) { let proto = Object.getPrototypeOf(obj); // start from [[Prototype]] while (proto) { if (proto === Constructor.prototype) return true; // found a match proto = Object.getPrototypeOf(proto); // move up } return false; // reached null - no match }

This is exactly how instanceof walks the prototype chain, until it finds a match.


Visually

javascript
function Animal() {} function Dog() {} Dog.prototype = Object.create(Animal.prototype); const rex = new Dog();

Chain:

javascript
rex → Dog.prototypeAnimal.prototypeObject.prototypenull

Checks:

javascript
console.log(rex instanceof Dog); // true console.log(rex instanceof Animal); // true console.log(rex instanceof Object); // true

Because all these prototypes appear in the rex.__proto__ chain.


Example with classes (same principle)

javascript
class Animal {} class Dog extends Animal {} const dog = new Dog(); console.log(dog instanceof Dog); // true console.log(dog instanceof Animal); // true console.log(dog instanceof Object); // true

The syntax is different, but under the hood it's the same prototype chain.


Example where instanceof returns false

javascript
function User() {} const tim = new User(); Object.setPrototypeOf(tim, null); console.log(tim instanceof User); // false (the chain is broken)

If an object has no prototype, or the chain does not contain User.prototype, the operator returns false.


The catch with different contexts (for example, iframes)

javascript
const arr = []; console.log(arr instanceof Array); // true // but in a different window/iframe: console.log(arr instanceof window.top.Array); // false

Because each context (frame, realm) has its own Array.prototype, and instanceof compares the object by reference, not by name.


Alternative: obj.constructor

javascript
const user = new User(); console.log(user.constructor === User); // true

But this is unreliable: the constructor property can be overridden:

javascript
user.constructor = null; console.log(user instanceof User); // still true

That's why instanceof is the safer approach.


Summary

CheckWhat it doesUses
obj instanceof ConstructorChecks whether Constructor.prototype is in the object's prototype chain[[Prototype]]
Returnstrue or false
More reliable than obj.constructorYes
Works with classesYes
Sensitive to context (iframe)Yes

Visual scheme

javascript
obj __proto__ → Constructor.prototype __proto__ → Object.prototype null

If Constructor.prototype appears on this path, instanceof returns true.


In one sentence:

instanceof checks whether the constructor's prototype is part of the object's prototype chain, walking up through __proto__ until it finds a match or null.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.