Skip to main content

Checking for a class instance

1. The instanceof Operator: The Main Way

javascript
class User {} const u = new User(); console.log(u instanceof User); // true

instanceof checks whether the prototype chain of the object u contains a reference to User.prototype.


Example:

javascript
class Animal {} class Dog extends Animal {} const dog = new Dog(); console.log(dog instanceof Dog); // true console.log(dog instanceof Animal); // true: inheritance works console.log(dog instanceof Object); // true: all objects inherit from Object

instanceof walks up the prototype chain, and if it finds a match with Class.prototype, it returns true.


2. How instanceof Works Under the Hood

javascript
obj instanceof Class

is roughly equivalent to this code:

javascript
let proto = Object.getPrototypeOf(obj); while (proto) { if (proto === Class.prototype) return true; proto = Object.getPrototypeOf(proto); } return false;

In other words, instanceof simply checks whether Class.prototype is present in the __proto__ chain.


3. An Example Where Inheritance Affects the Result

javascript
class A {} class B extends A {} class C extends B {} const c = new C(); console.log(c instanceof A); // true console.log(c instanceof B); // true console.log(c instanceof C); // true

The prototype chain:

javascript
c → C.prototype → B.prototype → A.prototype → Object.prototype → null

instanceof walks sequentially up the chain.


4. Checking Without instanceof: Through constructor

You can check whether an object's constructor matches the class:

javascript
class User {} const u = new User(); console.log(u.constructor === User); // true

But:

  • This approach breaks if the object was created via Object.create().
  • Or if someone changed prototype.constructor.
javascript
const fake = Object.create(User.prototype); console.log(fake.constructor === User); // true console.log(fake instanceof User); // true

Both approaches work as long as the prototype chain isn't broken.


5. An Alternative: Through Object.getPrototypeOf

javascript
class User {} const u = new User(); console.log(Object.getPrototypeOf(u) === User.prototype); // true

This works only for a direct instance. If there's inheritance, this method doesn't climb higher up the chain (unlike instanceof).


6. Checking Built-in Classes (Array, Date, Error, Map, etc.)

javascript
console.log([] instanceof Array); // true console.log([] instanceof Object); // true console.log(new Date() instanceof Date); // true console.log(new Date() instanceof Object); // true

instanceof works with built-in classes exactly the same way, since they are also built on prototypes.


7. Important When Working Across Different "Realms" (iframe, Worker, etc.)

If an object was created in a different context (for example, a different iframe), its Array or Object is a different constructor, so instanceof may return false.

javascript
// In a different iframe: console.log(arr instanceof window.Array); // false

The solution: use

javascript
Array.isArray(arr); // safe even across contexts

8. An Example With a Static Check Method

Sometimes a class gets its own check method:

javascript
class User { static isUser(obj) { return obj instanceof User; } } const u = new User(); console.log(User.isUser(u)); // true

Such a method is useful in large codebases, where types are often checked centrally.


Summary

MethodExampleWhat It ChecksSupports InheritanceNote
instanceofobj instanceof ClassThe __proto__ chainYesReliable
constructorobj.constructor === ClassThe object's constructorPartiallyBreaks if prototype was changed
Object.getPrototypeOfObject.getPrototypeOf(obj) === Class.prototypeThe direct prototypeNoOnly for the direct class
Static methodClass.isInstance(obj)Any logicYesConvenient in large projects

In short:

You can check that an object is an instance of a class like this:

javascript
obj instanceof Class

This operator:

  • walks the prototype chain (__proto__);
  • supports inheritance;
  • works with built-in classes too (Array, Date, Error);
  • but can be unreliable across contexts (iframe, Worker).

Short Answer

Interview ready
Premium

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