How to use === operator In Javascript
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”
// function to check string is empty or not
function checking(str){
// checking the string using === operator
if (str === "") {
console.log("Empty String")
}
else {
console.log("Not Empty String")
}
}
// calling the checking function with empty string
checking("")
// calling the checking function with not an empty string
checking("w3wiki")
Output:
Empty String
Not Empty String
How to check empty/undefined/null string in JavaScript?
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. To check for empty, undefined, or null strings in JavaScript, use conditional statements, string comparison methods, or the typeof operator to handle cases where strings lack content or value.
Here are some common approaches:
Table of Content
- Using === operator
- Using the length and ! operator
- Using replace