Introduction
JavaScript supports a variety of data types such as strings, numbers, floats, etc. A string is a collection of characters such as "John Doe". Typically, you create them by enclosing characters in double or single quotes. Alternatively, you can make a string by using the new String()
constructor:
let myString = 'John Doe';
let myString2 = new String("John Doe");
When performing specific operations, you may face a situation that demands you to verify that a specific variable is a string before processing it - lest an error be thrown. We'll cover that situation in this article! First, we'll take a look at how to check if a particular variable is a string in JavaScript and then show you an alternative approach that uses the Lodash library.
Standard Solution - Using typeof Operator
In JavaScript, the typeof
operator is the most used method to check the type of any variable. Alternatively, you can use the typeof()
method:
let myString = 'John Doe';
typeof myString; // string
typeof(myString); // string
If used with a string, the typeof
operator returns "string"
. Let's create a simple example to confirm this:
let myString = "John Doe";
if (typeof myString === "string") {
console.log("This variable is a string");
} else {
console.log("This variable is not a string");
}
Indeed, the myString
is a string:
This variable is a string
Note: Even if the variable contains a number that is wrapped in single/double quotes, it still would be considered a string.
One interesting problem with the typeof
operator is that it doesn't recognize strings created using the new String()
constructor. The new
keyword creates a new JavaScript object that is the instance of the String
type. Therefore, the typeof
operator won't correctly recognize strings created using the new String()
constructor:
let myString = new String('John Doe');
console.log(typeof myString); // "object"
In this case, instead of the typeof
operator, we need to use the instanceof
operator - it can detect that the object created with the new String()
constructor is the instance of the String
type:
let myString = new String("John Doe");
if (myString instanceof String) {
console.log("This variable is a string");
} else {
console.log("This variable is not a string");
}
Since the myString
is a string, this code will produce the following output:
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
This variable is a string
Using Lodash Library
If you're already using the Lodash library in your project, there's no harm in using it for checking whether a variable is a string or not! It's absolutely not necessary to have a dependency if we don't need Lodash for something else, but, if we already have that dependency, we can make use of the _.isString()
method, which returns true
if the specified value is a string primitive or a String
object, making it fit for both 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 is not a string");
}
Output:
This variable is a string
Conclusion
In this article, we've learned how to check if a variable is a string in JavaScript. Also, we've learned how this works with an external library like Lodash.