What is the use of the instanceof operator in JavaScript?

The instanceof operator is a built-in operator in JavaScript that is used to determine whether an object belongs to a particular class or constructor function.

The syntax of the instanceof operator is as follows:

object instanceof constructor;

Here, object is the object that you want to test, and constructor is the function that you want to test against. The instanceof operator returns true if object is an instance of constructor or one of its descendants, and false otherwise.

Here are some example of usages of instanceof operator:

Example 1: Using the instanceof operator with built-in types

You can use the instanceof operator to test whether an object is an instance of a built-in JavaScript type, such as Array, Date, RegExp, or Error.

Here is an example that demonstrates this:

let arr = [1, 2, 3];
let date = new Date();
let regExp = /hello/;
// Using the instanceof operator with built-in types
console.log(arr instanceof Array); // true
console.log(date instanceof Date); // true
console.log(regExp instanceof RegExp); // true
console.log(new Error() instanceof Error); // true
console.log([] instanceof Object); // true
console.log("" instanceof Object); // false
console.log(123 instanceof Object); // false

In this example, we create instances of the Array, Date, RegExp, and Error classes and use the instanceof operator to test whether each instance belongs to its corresponding class. We also test whether an empty array ([]) belongs to the Object class.

Note that the instanceof operator returns true if an object is an instance of a class or any of its subclasses. In the last three tests, the instanceof operator returns false because "" and 123 are not instances of any class that inherits from Object.

Example 2: Using the instanceof operator with custom classes

You can also use the instanceof operator to test whether an object is an instance of a custom class that you define.

For example:

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

  speak() {
    console.log(`${this.name} makes a noise.`);
  }
}

class Dog extends Animal {
  constructor(name) {
    super(name);
  }

  speak() {
    console.log(`${this.name} barks.`);
  }
}

let a = new Animal("animal");
let d = new Dog("dog");

console.log(a instanceof Animal); // true
console.log(a instanceof Object); // true
console.log(d instanceof Dog); // true
console.log(d instanceof Animal); // true
console.log(d instanceof Object); // true
console.log(new Animal() instanceof Dog); // false

In this example, we define two classes: Animal and Dog. The Dog class extends the Animal class and overrides the speak method. We create instances of both classes and use the instanceof operator to test whether each instance belongs to its corresponding class. We also test whether an instance of the Animal class belongs to the Dog class.

Example 3: Using the instanceof operator with interfaces

In JavaScript, there is no built-in support for interfaces. However, you can create interfaces using constructor functions or classes and use the instanceof operator to test whether an object implements an interface.

For example:

function Animal(name) {
  this.name = name;
}

Animal.prototype.speak = function () {
  console.log(`${this.name} makes a noise.`);
};

function Dog(name) {
  this.name = name;
}

Dog.prototype = Object.create(Animal.prototype);

function CanSpeak(animal) {
  return typeof animal.speak === "function";
}

let a = new Animal("animal");
let d = new Dog("dog");

console.log(CanSpeak(a)); // true
console.log(CanSpeak(d)); // true
console.log(CanSpeak({})); // false

In this example, we define two constructor functions: Animal and Dog. We add a speak method to the Animal prototype and inherit it in the Dog prototype. We then define an interface CanSpeak that tests whether an object has a speak method by checking whether the typeof the speak property is a function. We create instances of the Animal and Dog classes and test whether they implement the CanSpeak interface. Finally, we test whether an empty object implements the interface.

Note that this is not a true interface in the traditional sense of the word, as JavaScript does not have built-in support for interfaces. However, this pattern can be used to create a similar functionality in JavaScript.

Thank you for reading, and let’s have conversation with each other

Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.

Read more: