Introduction
JavaScript helps quite a lot of knowledge varieties similar to strings, numbers, floats, and so forth. A string is a set of characters similar to “John Doe”. Sometimes, you create them by enclosing characters in double or single quotes. Alternatively, you can also make a string through the use of the new String()
constructor:
let myString = 'John Doe';
let myString2 = new String("John Doe");
When performing particular operations, it’s possible you’ll face a state of affairs that calls for you to confirm {that a} particular variable is a string earlier than processing it – lest an error be thrown. We’ll cowl that state of affairs on this article! First, we’ll check out find out how to examine if a specific variable is a string in JavaScript after which present you another method that makes use of the Lodash library.
Customary Answer – Utilizing typeof Operator
In JavaScript, the typeof
operator is probably the most used technique to examine the kind of any variable. Alternatively, you need to use the typeof()
technique:
let myString = 'John Doe';
typeof myString;
typeof(myString);
If used with a string, the typeof
operator returns "string"
. Let’s create a easy instance to substantiate this:
let myString = "John Doe";
if (typeof myString === "string") {
console.log("This variable is a string");
} else {
console.log("This variable will not be a string");
}
Certainly, the myString
is a string:
This variable is a string
Notice: Even when the variable accommodates a quantity that’s wrapped in single/double quotes, it nonetheless can be thought-about a string.
One fascinating downside with the typeof
operator is that it does not acknowledge strings created utilizing the new String()
constructor. The new
key phrase creates a brand new JavaScript object that’s the occasion of the String
kind. Due to this fact, the typeof
operator will not appropriately acknowledge strings created utilizing the new String()
constructor:
let myString = new String('John Doe');
console.log(typeof myString);
On this case, as an alternative of the typeof
operator, we have to use the instanceof
operator – it may detect that the thing created with the new String()
constructor is the occasion of the String
kind:
let myString = new String("John Doe");
if (myString instanceof String) {
console.log("This variable is a string");
} else {
console.log("This variable will not be a string");
}
For the reason that myString
is a string, this code will produce the next output:
Try our hands-on, sensible information to studying Git, with best-practices, industry-accepted requirements, and included cheat sheet. Cease Googling Git instructions and truly study it!
This variable is a string
Utilizing Lodash Library
If you happen to’re already utilizing the Lodash library in your challenge, there isn’t any hurt in utilizing it for checking whether or not a variable is a string or not! It is completely not essential to have a dependency if we do not want Lodash for one thing else, however, if we have already got that dependency, we are able to make use of the _.isString()
technique, which returns true
if the desired worth is a string primitive or a String
object, making it match for each explicitly and implicitly created strings:
let myString = new String("John Doe");
if (_.isString(myString)) {
console.log("This variable is a string");
} else {
console.log("This variable will not be a string");
}
Output:
This variable is a string
Conclusion
On this article, we have discovered find out how to examine if a variable is a string in JavaScript. Additionally, we have discovered how this works with an exterior library like Lodash.