How to check if an object is string or not in JavaScript:
There are different ways in JavaScript to check if an object is string or not in JavaScript. This post will show you couple of different ways with examples.
Method 1: By using typeof and instanceof:
The typeof operator can be used to check the type of an operand. If we use it with a string variable, it will return “string”.
It is defined as like below:
typeof operand
typeof(operand)
If the operand is a string variable, it will return “string”. We can compare the returned value with “string” to check if the operand is a string or not.
It will work for any string variables. But, if we create a string object by using the new String() constructor, typeof will return “object”.
console.log(typeof 'hello'); // string
console.log(typeof new String('hello')); // object
To handle that, we can use instanceof. instanceof returns a boolean value and instanceof String will return true.
let testValues = [
"hello",
12,
12.34,
"12.34",
new String("hello"),
undefined,
NaN,
null,
" ",
];
testValues.forEach((e) => {
if (typeof e === "string" || e instanceof String) {
console.log(`${e} is a string`);
} else {
console.log(`${e} is not a string`);
}
});
It will print the below output:
hello is a string
12 is not a string
12.34 is not a string
12.34 is a string
hello is a string
undefined is not a string
NaN is not a string
null is not a string
is a string
We can also create a separate method to do the checking.
let testValues = [
"hello",
12,
12.34,
"12.34",
new String("hello"),
undefined,
NaN,
null,
" ",
];
function isString(e) {
return typeof e === "string" || e instanceof String;
}
testValues.forEach((e) => {
if (isString(e)) {
console.log(`${e} is a string`);
} else {
console.log(`${e} is not a string`);
}
});
Method 2: By using Object.prototype.toString.call():
The Object.prototype.toString.call method returns the string representation of the object. If the return value of Object.prototype.toString.call is [object string], it is a string. Else, it is not a string.
let testValues = [
"hello",
12,
12.34,
"12.34",
new String("hello"),
undefined,
NaN,
null,
" ",
];
function isString(e) {
return Object.prototype.toString.call(e) === '[object String]';
}
testValues.forEach((e) => {
if (isString(e)) {
console.log(`${e} is a string`);
} else {
console.log(`${e} is not a string`);
}
});
It will print the same output.
hello is a string
12 is not a string
12.34 is not a string
12.34 is a string
hello is a string
undefined is not a string
NaN is not a string
null is not a string
is a string
Method 3: Using lodash:
If your project is using lodash, you can use the method isString defined in it.
import _ from "lodash";
let testValues = [
"hello",
12,
12.34,
"12.34",
new String("hello"),
undefined,
NaN,
null,
" ",
];
testValues.forEach((e) => {
if (_.isString(e)) {
console.log(`${e} is a string`);
} else {
console.log(`${e} is not a string`);
}
});
It will give the same output.
String can be represented in many ways in JavaScript. We can use single quote, double quotes, backticks or even with the String class. Any of these methods will work.
You might also like:
- How to reverse an array in JavaScript in place
- How to add elements to the start of JavaScript arrays by using unshift
- JavaScript filter method explanation with example
- How to replace an item in an Array in JavaScript
- JavaScript parseFloat method explanation with examples
- JavaScript parseInt function example to convert a string to integer
- How to check if a number is NaN in JavaScript