instanceof and prototypes
the instanceof operator is directly tied to prototypes
and literally checks:
"Is the constructor's
prototypesomewhere in the object's prototype chain?"
Let's break it down step by step.
In short
Syntax:
obj instanceof ConstructorReturns true if
the prototype chain of the object obj
contains the object Constructor.prototype.
Otherwise it returns false.
Example
function User() {}
const tim = new User();
console.log(tim instanceof User); // true
console.log(tim instanceof Object); // true
console.log(tim instanceof Array); // falseUnder the hood instanceof checks:
tim.__proto__ === User.prototype
or
tim.__proto__.__proto__ === User.prototype
and so on until it reaches nullHow it works "under the hood"
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
function Animal() {}
function Dog() {}
Dog.prototype = Object.create(Animal.prototype);
const rex = new Dog();Chain:
rex → Dog.prototype → Animal.prototype → Object.prototype → nullChecks:
console.log(rex instanceof Dog); // true
console.log(rex instanceof Animal); // true
console.log(rex instanceof Object); // trueBecause all these prototypes appear in the rex.__proto__ chain.
Example with classes (same principle)
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); // trueThe syntax is different, but under the hood it's the same prototype chain.
Example where instanceof returns false
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)
const arr = [];
console.log(arr instanceof Array); // true
// but in a different window/iframe:
console.log(arr instanceof window.top.Array); // falseBecause each context (frame, realm) has its own Array.prototype,
and instanceof compares the object by reference, not by name.
Alternative: obj.constructor
const user = new User();
console.log(user.constructor === User); // trueBut this is unreliable: the constructor property can be overridden:
user.constructor = null;
console.log(user instanceof User); // still trueThat's why instanceof is the safer approach.
Summary
| Check | What it does | Uses |
|---|---|---|
obj instanceof Constructor | Checks whether Constructor.prototype is in the object's prototype chain | [[Prototype]] |
| Returns | true or false | |
More reliable than obj.constructor | Yes | |
| Works with classes | Yes | |
| Sensitive to context (iframe) | Yes |
Visual scheme
obj
↑
__proto__ → Constructor.prototype
↑
__proto__ → Object.prototype
↑
nullIf Constructor.prototype appears on this path,
instanceof returns true.
In one sentence:
instanceofchecks whether the constructor'sprototypeis part of the object's prototype chain, walking up through__proto__until it finds a match ornull.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.