How to use typeOf Operator In Javascript
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.
Example: This example shows the implementation of the above-explained approach.
let boolValue = true;
let numValue = 17;
let bool, num;
if (typeof boolValue == "string") {
bool = "is a string";
} else {
bool = "is not a string";
}
if (typeof numValue == "string") {
num = "is a string";
} else {
num = "is not a string";
}
console.log(bool);
console.log(num);
Output
is not a string is not 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