Skip to main content

How to Check if an Object is or is not an Instance of a Class in JavaScript

In object-oriented programming, it's often necessary to check an object's type before you can safely work with its properties or methods. The instanceof operator is the primary tool in JavaScript for this purpose. It provides a reliable way to determine if an object was created from a specific class or a class that inherits from it.

This guide will teach you how to use the instanceof operator to check an object's type, how to use it for "type guarding" in functions, and the correct way to check if an object is not an instance of a class.

The Core Method: The instanceof Operator

The instanceof operator is a simple boolean check that returns true if an object is an instance of a given class, and false otherwise.

Problem: you have an object and need to confirm it was created from a specific class.

Solution:

class Person {
constructor(name) {
this.name = name;
}
}

class Animal {}

const p1 = new Person('Alice');

// The instanceof operator returns a simple boolean.
console.log(p1 instanceof Person); // Output: true
console.log(p1 instanceof Animal); // Output: false

if (p1 instanceof Person) {
console.log('The object is an instance of the Person class.');
} else {
console.log('The object is NOT an instance of the Person class.');
}
// Output: The object is an instance of the Person class.

How instanceof Works (The Prototype Chain)

The instanceof operator doesn't just check the immediate constructor. It works by checking if the prototype property of the constructor function appears anywhere in the prototype chain of the object. This means it also works correctly with inheritance.

class Mammal {}
class Dog extends Mammal {} // Dog inherits from Mammal

const myDog = new Dog();

console.log(myDog instanceof Dog); // Output: true (Direct instance)
console.log(myDog instanceof Mammal); // Output: true (Instance of parent class)
console.log(myDog instanceof Object); // Output: true (All classes inherit from Object)

Using instanceof for Type Guarding

A major use case for instanceof is "type guarding"—checking the type of an object to safely access its specific methods or properties. This is especially common in languages like TypeScript, where the compiler can infer the type within the if block.

This function accepts two different types of objects and behaves differently based on which one it receives.

class Greeter {
greet() {
console.log('Hello!');
}
}
class Barker {
bark() {
console.log('Woof!');
}
}

function performAction(obj) {
if (obj instanceof Greeter) {
// Inside this block, we know `obj` has a `greet` method.
obj.greet();
} else if (obj instanceof Barker) {
// Inside this block, we know `obj` has a `bark` method.
obj.bark();
}
}

// Example Usage:
performAction(new Greeter()); // Output: Hello!
performAction(new Barker()); // Output: Woof!

How to Check if an Object is NOT an Instance of a Class

To check if an object is not an instance of a class, you use the logical NOT (!) operator to negate the result of the instanceof check.

It is crucial to wrap the instanceof expression in parentheses to ensure the correct order of operations.

class Person {}
class Animal {}

const p1 = new Person();

// The parentheses are important!
if (!(p1 instanceof Animal)) {
console.log('Correct: p1 is not an instance of Animal.');
} else {
console.log('Incorrect: p1 is an instance of Animal.');
}
// Output: Correct: p1 is not an instance of Animal.

The Common Pitfall

Forgetting the parentheses leads to a common bug. The ! operator has higher precedence than instanceof, so it acts on p1 first.

// This is WRONG!
if (!p1 instanceof Animal) { /* ... */ }

// The above is evaluated as:
// 1. `!p1` becomes `false` (since an object is "truthy").
// 2. `false instanceof Animal` is always `false`.
// This logic is flawed and will not work as intended.

Conclusion

The instanceof operator is the standard and most reliable tool for checking an object's type in JavaScript.

  • Use object instanceof Class to get a true/false result. This is the primary method for type checking.
  • It works correctly with inheritance, checking the entire prototype chain.
  • To check if an object is not an instance, use the negated and parenthesized pattern: !(object instanceof Class).

By using instanceof, you can write robust, object-oriented code that safely interacts with objects based on their type.