There are different ways to check if a string variable is empty in JavaScript.

Solution 1:

We can use triple equals operator, which checks for exactly equals to along with type in JavaScript

Copy to Clipboard

If you use double equals instead of triple equals, even if the variable value is false or 0, it satisfies the condition. We get myString is Empty and myString2 is Empty for the following conditions

example:

Copy to Clipboard

But the same thing does not happen with the other falsy things of JavaScript, undefined, null and NaN. We get false in the console for all the following three conditions

Copy to Clipboard

Solution 2:

By checking if the variable is falsy and its length is 0.

Copy to Clipboard

Why to check if the variable is falsy, isn’t checking the length alone is not sufficient ?

Its not sufficient, because, if the variable is an empty array instead of a string, even that variable satisfies the condition. But an empty array is not a falsy thing in JavaScript, so it do not satisfies the first condition. We get false for the code below

Copy to Clipboard