![]() |
VOOZH | about |
Empty strings contain no characters, while null strings have no value assigned. Checking for an empty, undefined, or null string in JavaScript involves verifying if the string is falsy or has a length of zero. Here are different approaches to check a string is empty or not.
Using === operator we will check the string is empty or not. If empty then it will return "Empty String" and if the string is not empty it will return "Not Empty String".
Syntax
if (str === "") {
console.log("Empty String")
} else {
console.log("Not Empty String")
}
Empty String Not Empty String
This approach uses length property to get the length of string and ! operator. and by using ! operator we will check string is empty or not.
true false
This approach uses replace() Method. It will ensure that the string is not just a group of empty spaces where we are doing replacement on the spaces.
Empty String Not Empty String