Falsey values in JavaScript

One dangerous issue of falsey values you have to be aware of is when checking the presence of a certain property.

Suppose you want to test for the availability of a new property; when this property can actually have a value of 0 or "", you can’t simply check for its availability using

if (!someObject.someProperty)
    /* incorrectly assume that someProperty is unavailable */

In this case, you must check for it being really present or not:

if (typeof someObject.someProperty == "undefined")
    /* now it's really not available */

Also be aware that NaN isn’t equal to anything, even not to itself (NaN != NaN).

Leave a Comment