DEV Community

Codecupdev
Codecupdev

Posted on

Learn how to use instanceof in JavaScript


Introduction
In JavaScript instanceof is an operator. An operator is either used for calculations or for logic. The instanceof operator will see if an operand (what we perform the operation on) is an instance of the value we pass. When we use instance of we place the operand on the left and the value we pass on the right. This is shown in the example below:

someoperand instanceof somevalue

Enter fullscreen mode Exit fullscreen mode

Examples
When we use the instanceof operator it will check whether the operand is an instance of the value we pass and it will also check the prototype chain of the value. The return value will always be a Boolean (true or false). Let’s start by looking at some basic examples.

const pets = {
  amount: 4
};
pets instanceof String
//Returns ---> false
pets instanceof Number
//Returns ---> false
pets instanceof Object
//Returns ---> true

Enter fullscreen mode Exit fullscreen mode

In the above example we create an object literal stored in a variable called pets. Inside of the pets object we create a property called amount with a value of four. Next we use the instanceof operator to check whether pets is an instance of String, Number and Object. Both String and Number return false. Even though the value stored inside of pets is a number we are checking the the object itself not the properties inside of the object. As pets is an object when we then go ahead and check whether pets is an instance of Object we get true.

Let’s look at another example.

const greeting = "Hello"
greeting instanceof String
//Returns false

Enter fullscreen mode Exit fullscreen mode

In the above example we declare a variable called greeting and assign to this the string Hello. Next we check whether greeting is an instance of String. We get false. This may surprise you. When we use the instanceof operator we are checking whether it is an instance of the String object we are not checking the type. When we create the greeting variable we use a string literal so it is not actually an instance of String. If we wanted this to return true we would need to do the following.

const greeting = new String("Hello")
greeting instanceof String
//Returns ---> true
Enter fullscreen mode Exit fullscreen mode

The instanceof operator can be useful when you are working with classes and you want to check whether something is an object of a class. Let’s look at an example of this.

class Car {}
const honda = new Car()
honda instanceof Car
//Returns ---> true
Enter fullscreen mode Exit fullscreen mode

I will wrap up this article with one last example.

const arr = [1, 2, 3]
arr instanceof Array
//Returns ---> true
arr instanceof Object
//Returns ---> true

Enter fullscreen mode Exit fullscreen mode

In the above example i declare a variable called arr and I assign to this an array containing some values. Next using the instanceof operator I check whether arr is an instanceof Array. It is so I get true returned. Next i check whether it is an instance of Object. I also get true returned. You can expect functions to work in a similar way. The reason for this is that they inherit from Object in the prototype chain.

I hope you enjoyed this article. Please feel free to post any comments, questions, or feedback, and follow me for more content!

Top comments (0)