- “” (empty string)
- NaN (Not a Number)
Comparing Falsy and Truthy values
The need for learning these values is because of its use in comparison. The comparison is where these values creates many troubles. Misinterpretation of any value may lead to the wrong action in a script. Therefore it is necessary to check for truthy and falsy values correctly.
The Falsy values 0, “” (empty string) and false are equal to each other and comparison among them results in a true value.
Null and undefined are both falsy values but are not equal to any value, not even falsy. A bit strange but that’s true.
Null and undefined are equal to themselves.
isNaN() function. This function can handle the expressions returning NaN.
To avoid the logical error in a script, you can use triple equal (===) or triple not equal(!==). Unlike a double equal (==) that uses a value for the equality check, triple equal (===) checks on the basis of value and type.
From the preceding article, I can suggest that you always use a triple equal since it is difficult to memorize the rules of double equal and truthy falsy comparison results. Thanks for reading this article. Don’t forget to share and comment.
Filed under: CodeProject