Who Created This Object?

The instanceof operator checks whether an object was created by a particular class — or by any class in its inheritance chain.

class Animal {}
class Rabbit extends Animal {}

let rabbit = new Rabbit();

console.log(rabbit instanceof Rabbit); // true
console.log(rabbit instanceof Animal); // true — due to inheritance!
console.log(rabbit instanceof Object); // true — everything inherits from Object

How It Works

instanceof walks up the prototype chain looking for a match with the class's prototype property.

Customizing instanceof Behavior

You can override the default behavior using Symbol.hasInstance:

class Edible {
  static [Symbol.hasInstance](obj) {
    return obj.canEat === true; // "Edible" if it has canEat
  }
}

console.log({ canEat: true } instanceof Edible); // true — no class needed!
Note

This is useful when you want to check " duck-typing " (does it have the right properties?) rather than strict class origin.

Caution

instanceof can give incorrect results if your page uses multiple iframes or windows, because each window has its own separate copies of global classes (Array, Object, etc.).