How to use Instanceof Operator In Javascript

The instanceof operator in JavaScript is used to check the type of an object at run time.

Syntax:

let gfg = objectName instanceof objectType

Example: This example shows the implementation of above-explained appraoch.

Javascript
let variable = new String();

if (variable instanceof String) {
  console.log("variable is a string.");
} else {
  console.log("variable is not a string.");
}

Output
variable is a string.

Check if a variable is a string using JavaScript

In this article, we will check if a variable is a string using JavaScript. We can check the variable by using many methods.

Below are the following methods through which we can check the type of variable is a string:

Table of Content

  • Using typeOf Operator
  • Using Instanceof Operator
  • Underscore.js _.isString()
  • Using Lodash _.isString() Method
  • Using Object.prototype.toString.call Method

Similar Reads

Using typeOf Operator

In this approach, we are using typeOf operator which tells us the type of the given value. we are getting the type of the value by the use of the typeOf operator and we are using if-else for returning our ans....

Using Instanceof Operator

TheĀ instanceof operatorĀ in JavaScript is used to check the type of an object at run time....

Underscore.js _.isString()

TheĀ _.isString() functionĀ is used to check whether the given object element is string or not....

Using Lodash _.isString() Method

In this approach, we are using the Lodah _.isString() method that returns boolean response for the given value....

Using Object.prototype.toString.call Method

The Object.prototype.toString.call method is a reliable way to determine the type of a variable. It returns a string indicating the type of the object....

Contact Us