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.
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
Contact Us