Check if Object is or is NOT instanceof Class in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 2, 2024
4 min

banner

# Table of Contents

  1. Check if Object is an instance of Class in JavaScript
  2. Check if an Object is NOT an instanceof a Class in JavaScript

If you need to check if an object is NOT an instance of a class, click on the second subheading.

# Check if Object is an instance of Class in JavaScript

Use the instanceof operator to check if an object is an instance of a class.

The instanceof operator will return true if the object is an instance of the class and false otherwise.

index.js
class Person {} const p1 = new Person(); if (p1 instanceof Person) { console.log('✅ is instance of Person'); } else { console.log('⛔️ is not instance of Person'); } class Animal {} console.log(p1 instanceof Animal); // 👉️ false

check if object is instance of class

The code for this article is available on GitHub

The instanceof operator returns a boolean value indicating whether the prototype property of the constructor appears in the prototype chain of the object.

The p1 object was created using the Person class, so it is an instance of the class.

Here is an example that uses the instanceof operator to determine which method is available on an object.

index.ts
class Person { walk() { console.log('person is walking'); } } class Animal { run() { console.log('animal is running'); } } function example(x) { // 👉️ x is type Person or Animal here if (x instanceof Person) { // 👇️ x is type Person here x.walk(); } else { // 👇️ x is type Animal here x.run(); } } const p1 = new Person(); const a1 = new Animal(); example(p1); // 👉️ "person is walking" example(a1); // 👉️ "animal is running"

using instanceof operator to check which method is available

The function takes a parameter of type Person or Animal, so before we access a class-specific method, we have to check an instance of which class was passed to the function.

If you access the constructor.name property, you can see that the class name of the p1 object is Person.

index.ts
class Person {} const p1 = new Person(); console.log(p1.constructor.name); // 👉️ Person

We accessed the name property on the Object.constructor property.

The Object.constructor property returns a reference to the constructor function, from which the object was created.

The instanceof operator checks for the presence of constructor.prototype in the object's prototype chain.

index.ts
class Person {} const p1 = new Person(); // 👇️ true console.log(Object.getPrototypeOf(p1) === Person.prototype);

# Check if an Object is NOT an instanceof a Class in JavaScript

Alternatively, you can use the logical NOT (!) operator.

The logical NOT (!) operator inverts the value the instanceof operator returns.

index.js
class Person {} class Animal {} const p1 = new Person(); if (!(p1 instanceof Animal)) { console.log('✅ Object is not instance of Animal'); } else { console.log('⛔️ Object is instance of Animal'); }

check if object is not instance of class

The code for this article is available on GitHub

The instanceof operator returns a boolean value indicating if the prototype property of the constructor appears in the prototype chain of the object.

Notice that we used parentheses around the instanceof check before negating it.

A commonly seen mistake is to omit the parentheses, e.g.:

index.js
// ❌ Don't do this console.log(!p1 instanceof Animal); // 👉️ false

The code sample inverts the value of the object and converts it to a boolean, so it becomes false.

index.js
class Person {} const p1 = new Person(); console.log(!p1); // 👉️ false

We then check if false is an instance of the Animal class which will always return false.

Wrapping the expression in parentheses allows us to evaluate it as a whole.
index.js
class Person {} class Animal {} const p1 = new Person(); if (!(p1 instanceof Animal)) { console.log('✅ Object is not instance of Animal'); } else { console.log('⛔️ Object is instance of Animal'); }

The p1 instanceof Animal expression is first evaluated and returns false if the object is not an instance of the class.

Then the logical NOT (!) operator inverts the value to true and the if block runs.

If the object is an instance of the class, the else block runs.

If you don't like the parentheses approach, compare the value to the boolean false.

# Check if an Object is NOT an instanceof a Class using boolean comparison

This is a three-step process:

  1. Use the instanceof operator to check if the object is an instance of the class.
  2. Compare the output of the expression to false.
  3. If the instanceof operator returned false, the object is not an instance of the class.
index.js
class Person {} class Animal {} const p1 = new Person(); if (p1 instanceof Animal === false) { console.log('✅ Object is not instance of Animal'); } else { console.log('⛔️ Object is instance of Animal'); }

check if object is not instance of class using boolean comparison

The code for this article is available on GitHub

We first used the instanceof operator to test if the p1 object is an instance of the Animal class.

If the test returns false, then the object is not an instance of the class.

If the object is not an instance of the class, the if block runs, otherwise, the else block runs.

This approach achieves the same goal as using the logical NOT (!) operator with parentheses.

However, it might be a bit more readable because you don't have to think about the order in which the statements are run.

Here are some more examples of using the logical NOT (!) operator.

index.js
console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'str'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true

The logical NOT (!) operator converts truthy values to false and fasly values to true.

The falsy values in JavaScript are: false, 0, -0, "" (empty string), null, undefined, NaN (Not a number).

When used with any other than the aforementioned 6 values, the logical NOT (!) operator returns false.

If you decide to use the logical NOT (!) operator approach, make sure to wrap the instanceof test in parentheses.

index.js
class Person {} class Animal {} const p1 = new Person(); if (!(p1 instanceof Animal)) { console.log('✅ Object is not instance of Animal'); } else { console.log('⛔️ Object is instance of Animal'); }

wrap instance of test in parentheses

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.