• Register
1,240 points
10 7 4

We can store ‘Kodlogs’ like data in strings. Strings contain letters, numbers, symbols and all other letters. String can be either in single quotes or double quotes. 
This article discusses the method to check if a string is empty or not.

How to check for an empty string?

To check if the string is empty or not, we can match the data of the string. If the data is equal to “” it means the string is empty, otherwise it will return false. 
If we use “==” operator to check if the string is empty then we can get wrong output some time because “==” operator will look for “” in string and it will return true even if the string has some data in it.
To resolve this issue and to check correctly if the string is empty, we have to use the “===” strict equality operator. Strict equality operators will only return true if the data type of the value is string and it is not empty.


if(str === ""){
    // string is empty, do something
// Some test cases
alert(2 === "");  // Outputs: flase
alert(0 === "") // Outputs: false
alert("" === "") // Outputs: true
alert("Hello World!" === "") // Outputs: false 
alert(false === "") // Outputs: false
alert(null === "") // Outputs: false
alert(undefined === "") // Outputs: false

As we can see in the above code the null, undefined, false returns false in the comparison since they are special values.

Hope this will help.