JavaScript

How to Use Instanceof Operator in JavaScript

Every JavaScript object comprises a prototype, which can be accessed using the “__proto__ property”. This property is also associated with functions that set the initial property for the created object of the given type. When a function is defined in a JavaScript program, a unique prototype is assigned to it. You can use the JavaScript “instanceof” operator to determine whether an object is an instance of a class or a built function by checking its prototype.

This post will discuss the different use cases of the “instanceof” operator in JavaScript. So, let’s start!

JavaScript instanceof operator

The “instanceof” operator in JavaScript is utilized for checking the object “type” according to the specified “class” at “run time”. This operator returns a “boolean” value, where “true” indicates that the mentioned object is an instance of the specified JavaScript class, and “false” represents the negation.

Syntax

object instanceof class

Now, have a look at the below-given examples to implement the “instanceof” operator in a JavaScript program.

How to use instanceof operator in JavaScript to check String Type

First of all, we will create a “String” type object named “str” with the following value:

var str = new String("Alex");

Next, we will use the “instanceof” operator to check if “str” is an instance of the “String” class or not:

str instanceof String;

Execution of the above-given statement will return “true” as “str” comprises a string type value and is an object of the “String” class:

How to use instanceof operator in JavaScript to check Number Type

Similarly, you can utilize the “instanceof” operator to check if a created number variable is an instance of the “Number” class or not:

var num = new Number("2022");

num instanceof Number;

Output

How to use instanceof operator in JavaScript to check Array type

In the below-given JavaScript program, we will create an array name “arr” having some string values:

var arr = [ "HTML", "Python", "C#", "CSS", "Java", "JavaScript"];

Then, we will use the “instanceof” operator to check if the “arr” instance is a type of the JavaScript “Array” class:

arr instanceof Array;

The returned value is “true,” which signifies that “arr” is an Array instance:

How to use instanceof operator in JavaScript with Constructor functions

The “instanceof” operator is utilized to verify the object type of “Constructor functions”. For instance, in the following example, we will create a constructor function named “Employee” that accepts a “name” argument:

function Employee(name) {

  this.name = name

}

Next, we will create an “employee1” of the “Employee” type while passing “Alex” as the “name” argument value:

let employee1= new Employee("Alex");

Lastly, we will utilize the “instanceof” JavaScript operator to check whether “employee1” is an instance of “Employee”:

console.log(employee1 instanceof Employee)

Output

How to use instanceof operator in JavaScript with Inheritance

JavaScript also offers “Prototype Inheritance,” used to add methods and properties to an object. In this hierarchy, the “instanceof” operator is utilized to validate if the specified JavaScript object is an instance of the mentioned class or not.

For instance, we will create an “Employee” class that “extends” “Person” class as its “parent” class:

class Person {}

class Employee extends Person {

  constructor(name) {

    super()

    this.name = name

  }

}

After doing so, we will create an instance of the “Employee” class and use the “instanceof” operator to check if “employee” is considered an instance of both the “Person” and “Employee” classes or not:

let employee1= new Employee("Alex");

console.log(employee1 instanceof Person)

console.log(employee1 instanceof Employee)

The given output signifies that JavaScript marked “employee” as an instance of both classes because of inheritance:

JavaScript beginners often get confused between the functionality of the “instanceof”  and “typeof” operators. To clear out your concept related to the mentioned operators, check out the following section.

JavaScript instanceof operator vs typeof operator

The ”typeof” JavaScript operator outputs a ”string” representing the “type” of the value. It is mostly used for built-in JavaScript types.

For instance, In the below-given program, the “typeof” operator will return “string” as a type of the “linuxhint” value and “number” for the “232” value:

console.log(typeof "linuxhint");

console.log(typeof 232);

However, with the “instanceof” operator, you must mention the type or class for which the specified value is tested. This operator returns a “true” or “false” boolean value which depends on the result of validation. More specifically, the “instanceof” JavaScript operator is utilized for testing the “custom” and “advanced” types, whereas “typeof” operator is used to verify the “common” or “built-in” JavaScript data types.

That was all essential information regarding the JavaScript “instanceof” operator. You can further research as required.

Conclusion

The “instanceof” operator in JavaScript is utilized for checking the object “type” according to the specified class at “run time”. This operator returns a “boolean” value, where “true” indicates that the mentioned object is an instance of the specified JavaScript class, and “false” represents the negation. The JavaScript “instanceof” operator is to verify custom and advanced data types. This post discussed different use cases of the “instanceof” operator in JavaScript.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.