[javascript] Javascript: How to check if a string is empty?

I know this is really basic, but I am new to javascript and can't find an answer anywhere.

How can I check if a string is empty?

This question is related to javascript string

The answer is


I check length.

if (str.length == 0) {
}

But for a better check:

if(str === null || str === '')
{
    //enter code here
}

This should work:

if (variable === "") {

}

if (value == "") {
  // it is empty
}

If you want to know if it's an empty string use === instead of ==.

if(variable === "") {
}

This is because === will only return true if the values on both sides are of the same type, in this case a string.

for example: (false == "") will return true, and (false === "") will return false.