From http://www.sitepoint.com/javascript-truthy-falsy/
The following values are always falsy:
- false
- 0 (zero)
- “” (empty string)
- null
- undefined
- NaN (a special Number value meaning Not-a-Number!)
All other values are truthy, including "0"
(zero in quotes), "false"
(false in quotes), empty functions, empty arrays ([]
), and empty objects ({}
).
Regarding why this is so, I suspect it’s because JavaScript arrays are just a particular type of object. Treating arrays specially would require extra overhead to test Array.isArray()
. Also, it would probably be confusing if true arrays behaved differently from other array-like objects in this context, while making all array-like objects behave the same would be even more expensive.