How instanceof Works
MedThe instanceof operator checks if an object appears in the prototype chain of another object. Specifically, object instanceof Constructor checks if Constructor.prototype exists anywhere in object's prototype chain. This is how JavaScript implements type checking for constructor-created objects.
Interactive Visualization
dog instanceof Animal
Prototype Chain
dog
instance
__proto__
Animal.prototype
prototype object
__proto__
Object.prototype
prototype object
__proto__
null
end of chain
...
setup
Target (looking for)
Animal.prototype
Animal.prototype
from: dog instanceof Animal
Evaluating: dog instanceof Animal
1 / 4
Key Insight: instanceof returns true when Constructor.prototype is found anywhere in the object's prototype chain.
Key Points
- obj instanceof Constructor checks prototype chain
- Returns true if Constructor.prototype is in obj's chain
- Walks up __proto__ links looking for prototype property
- Does not work across iframes (different realms)
- Can be overridden with Symbol.hasInstance
Code Examples
Basic instanceof
function Animal(name) { this.name = name; } const dog = new Animal("Rex"); console.log(dog instanceof Animal); // true dog.__proto__ === Animal.prototype; // true // instanceof checks: // Is Animal.prototype in dog's prototype chain? // dog → Animal.prototype → Object.prototype → null // Yes! Found at first link. console.log(dog instanceof Object); // true // Object.prototype is also in the chain
instanceof checks if the constructor's prototype property appears in the object's prototype chain.
Manual instanceof Implementation
function myInstanceOf(obj, Constructor) { // Get the prototype we're looking for const targetPrototype = Constructor.prototype; // Walk up the prototype chain let current = Object.getPrototypeOf(obj); while (current !== null) { if (current === targetPrototype) { return true; } current = Object.getPrototypeOf(current); } return false; } function Animal() {} const dog = new Animal(); console.log(myInstanceOf(dog, Animal)); // true console.log(myInstanceOf(dog, Object)); // true
instanceof walks up the prototype chain looking for the constructor's prototype property.
Cross-Realm Issue
// In main window const arr = []; arr instanceof Array; // true // In iframe (different realm) const iframe = document.createElement('iframe'); document.body.appendChild(iframe); const iframeArray = iframe.contentWindow.Array; const arr2 = new iframeArray(); // Problem: different realms have different Array constructors arr2 instanceof Array; // false! arr2 instanceof iframeArray; // true // Solutions: Array.isArray(arr2); // true ✅ Works across realms Object.prototype.toString.call(arr2); // "[object Array]"
instanceof fails across iframes/windows because each realm has its own constructor functions.
Common Mistakes
- Thinking instanceof checks the constructor that created the object
- Not realizing it fails across iframes
- Confusing it with typeof
Interview Tips
- Know that instanceof checks the prototype chain
- Understand the iframe limitation
- Know alternatives: Array.isArray(), Object.prototype.toString
- Be able to implement instanceof manually