To check if a String is empty in JavaScript, you can use any of the following methods –
- Use the === operator
- Use the typeof and length() method
- Use the typeof and str!==”” method
Let’s take a look at each of these methods in detail.
Check If A String Is Empty In JavaScript
The === Operator
You can use the === operator to check if a string is empty in JavaScript. This is the most recommended method as it makes your code more readable and understandable. The str === “” method returns true only if the type of the variable is String and value is “”.
Consider the following example –
var str = "";
if(str === "") {
console.log("String is empty");
} else {
console.log("String is not empty");
}
In the above example, we have declared a string variable named str with an empty value. We are using the === operator to check if the string is empty.
If the value of str is an empty string, then it will return true and print “String is empty” otherwise it will return false and print “String is not empty”.
If the string contains only spaces, we can use the trim() method to remove them before using the === operator.
For example,
var str = " ";
if(str.trim() === "") {
console.log("String is empty");
} else {
console.log("String is not empty");
}
In the above example, we have declared a string variable named str with value as three spaces. We are using the trim() method to remove the spaces before using the === operator.
If the value of str is an empty string after trimming, then it will return true and print “String is empty” otherwise it will return false and print “String is not empty”.
Use The typeof And length() Method
Another way to check if a string is empty in JavaScript is to use the typeof and length() method. The typeof method is used to get the data type of a variable. It returns “string” if the data type of the variable is string otherwise it returns other data types like number, boolean, undefined, null etc.
The length() method is used to get the length of a string. It returns 0 if the string is empty otherwise it returns a positive integer.
Consider the following example –
var str = "";
if(typeof str === "string" && str.length === 0) {
console.log("String is empty");
} else {
console.log("String is not empty");
}
In the above example, we have declared a string variable named str with an empty value. We are using the typeof and length() method to check if the string is empty.
If the data type of str is string and its length is 0, then it will return true and print “String is empty” otherwise it will return false and print “String is not empty”.
If the string has only spaces, we can use the trim() method to remove them before using the typeof and length method.
For example,
var str = " ";
if(typeof str === "string" && str.trim().length === 0) {
console.log("String is empty");
} else {
console.log("String is not empty");
}
In the above example, we have declared a string variable named str with value as three spaces. We are using the trim() method to remove the spaces before using the typeof and length() method.
If the data type of str is string and its length is 0 after trimming, then it will return true and print “String is empty” otherwise it will return false and print “String is not empty”.
Use The typeof and str!== “” Method
Another way to check if a string is empty in JavaScript is to use the typeof and str!==”” method. The str!==”” method returns true if the value of the string is not an empty string.
Consider the following example –
var str = "";
if(typeof str === "string" && str !== "") {
console.log("String is not empty");
} else {
console.log("String is empty");
}
In the above example, we have declared a string variable named str with an empty value. We are using the typeof and str!==”” method to check if the string is empty.
If the data type of str is string and its value is not an empty string, then it will return true and print “String is not empty” otherwise it will return false and print “String is empty”.
If the string has only spaces, we can use the trim() method to remove them before using the typeof and str!== “” method.
For example,
var str = " ";
if(typeof str === "string" && str.trim() !== "") {
console.log("String is not empty");
} else {
console.log("String is empty");
}
In the above example, we have declared a string variable named str with value as three spaces. We are using the trim() method to remove the spaces before using the typeof and str!== “” method.
If the data type of str is string and its value is not an empty string after trimming, then it will return true and print “String is not empty” otherwise it will return false and print “String is empty”.
Conclusion
In this article, we have discussed different ways to check if a string is empty in JavaScript. All the methods discussed above are used to check for an empty string. You can choose any method depending on your requirement.
Thank you for reading! We hope this article has been helpful.