The instanceof operator?
The instanceof operator in JavaScript is used to check
whether an object is an instance of a given class or constructor.
In other words, it checks which "prototype chain" the object belongs to.
Syntax
object instanceof Constructorobject- the object to check.Constructor- a constructor function or a class.
Returns:
trueorfalse
Simple example
class User {}
const user = new User();
console.log(user instanceof User); // true
console.log(user instanceof Object); // true (all objects inherit from Object)
useris an instance ofUserAnd alsouseris an instance ofObject, becauseUserinherits fromObject
How this works under the hood
instanceof checks the prototype chain:
- takes
object.__proto__ - compares it with
Constructor.prototype - if it does not match, it moves up the prototype chain (
object.__proto__.__proto__, and so on) - if a match is eventually found →
true, otherwise →false
Illustration:
function Animal() {}
const dog = new Animal();
// Check:
console.log(dog instanceof Animal); // true
// Under the hood:
console.log(Object.getPrototypeOf(dog) === Animal.prototype); // trueExample with inheritance
class Animal {}
class Dog extends Animal {}
const rex = new Dog();
console.log(rex instanceof Dog); // true
console.log(rex instanceof Animal); // true
console.log(rex instanceof Object); // true
console.log(rex instanceof Array); // false
instanceof"climbs" the entire inheritance chain:Dog → Animal → Object
Example with regular constructor functions
function Car() {}
function Bike() {}
const tesla = new Car();
console.log(tesla instanceof Car); // true
console.log(tesla instanceof Bike); // falseExample with arrays
const arr = [1, 2, 3];
console.log(arr instanceof Array); // true
console.log(arr instanceof Object); // trueAll arrays are objects, so
arr instanceof Objectis alsotrue.
Important: instanceof does not work with primitives
console.log(123 instanceof Number); // false
console.log('hello' instanceof String); // falseBecause
123and'hello'are primitives, not objects. (Butnew Number(123)will be an object.)
Example with wrapper objects
const n = new Number(123);
console.log(n instanceof Number); // true
const s = new String('hi');
console.log(s instanceof String); // trueBut in real code, wrappers (
new Number,new String) are almost never used, it's better to use primitives.
Custom checks
Sometimes instanceof won't work if the object came from a different context (iframe, window),
because it has its own Array prototype.
Example:
iframe.contentWindow.Array !== window.ArraySo
instanceofcan returnfalse, even if it really is an array.
In such cases, it's better to use:
Array.isArray(value);In short
| Checks | Whether the object is in the constructor's prototype chain |
|---|---|
| Returns | true / false |
| Works with | Objects (not primitives) |
| Checks against | Constructor.prototype |
| Accounts for inheritance | Yes |
| Suitable for arrays, classes, functions | Yes |
Reinforcement example
class Person {}
class Developer extends Person {}
const tim = new Developer();
console.log(tim instanceof Developer); // true
console.log(tim instanceof Person); // true
console.log(tim instanceof Object); // true
console.log(tim instanceof Array); // falseShort Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.