Checking for a class instance
1. The instanceof Operator: The Main Way
class User {}
const u = new User();
console.log(u instanceof User); // trueinstanceof checks
whether the prototype chain of the object u contains a reference to User.prototype.
Example:
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 Objectinstanceof walks up the prototype chain,
and if it finds a match with Class.prototype, it returns true.
2. How instanceof Works Under the Hood
obj instanceof Classis roughly equivalent to this code:
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
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); // trueThe prototype chain:
c → C.prototype → B.prototype → A.prototype → Object.prototype → nullinstanceof walks sequentially up the chain.
4. Checking Without instanceof: Through constructor
You can check whether an object's constructor matches the class:
class User {}
const u = new User();
console.log(u.constructor === User); // trueBut:
- This approach breaks if the object was created via
Object.create(). - Or if someone changed
prototype.constructor.
const fake = Object.create(User.prototype);
console.log(fake.constructor === User); // true
console.log(fake instanceof User); // trueBoth approaches work as long as the prototype chain isn't broken.
5. An Alternative: Through Object.getPrototypeOf
class User {}
const u = new User();
console.log(Object.getPrototypeOf(u) === User.prototype); // trueThis 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.)
console.log([] instanceof Array); // true
console.log([] instanceof Object); // true
console.log(new Date() instanceof Date); // true
console.log(new Date() instanceof Object); // trueinstanceof 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.
// In a different iframe:
console.log(arr instanceof window.Array); // falseThe solution: use
Array.isArray(arr); // safe even across contexts8. An Example With a Static Check Method
Sometimes a class gets its own check method:
class User {
static isUser(obj) {
return obj instanceof User;
}
}
const u = new User();
console.log(User.isUser(u)); // trueSuch a method is useful in large codebases, where types are often checked centrally.
Summary
| Method | Example | What It Checks | Supports Inheritance | Note |
|---|---|---|---|---|
instanceof | obj instanceof Class | The __proto__ chain | Yes | Reliable |
constructor | obj.constructor === Class | The object's constructor | Partially | Breaks if prototype was changed |
Object.getPrototypeOf | Object.getPrototypeOf(obj) === Class.prototype | The direct prototype | No | Only for the direct class |
| Static method | Class.isInstance(obj) | Any logic | Yes | Convenient in large projects |
In short:
You can check that an object is an instance of a class like this:
javascriptobj instanceof ClassThis 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 readyA concise answer to help you respond confidently on this topic during an interview.