What are the values in JavaScript that are 'falsey', meaning that they evaluate as false in expressions like if(value), value ? and !value?
There are some discussions of the purpose of falsey values on Stack Overflow already, but no exhaustive complete answer listing what all the falsey values are.
I couldn't find any complete list on MDN JavaScript Reference, and I was surprised to find that the top results when looking for a complete, authoritative list of falsey values in JavaScript were blog articles, some of which had obvious omissions (for example, NaN), and none of which had a format like Stack Overflow's where comments or alternative answers could be added to point out quirks, surprises, omissions, mistakes or caveats. So, it seemed to make sense to make one.
4 Answers
Falsey values in JavaScript
false- Zero of
Numbertype:0and also-0,0.0, and hex form0x0(thanks RBT) - Zero of
BigInttype:0nand0x0n(new in 2020, thanks GetMeARemoteJob) "",''and``- strings of length 0nullundefinedNaNdocument.all(in HTML browsers only)- This is a weird one.
document.allis a falsey object, withtypeofasundefined. It was a Microsoft-proprietory function in IE before IE11, and was added to the HTML spec as a "willful violation of the JavaScript specification" so that sites written for IE wouldn't break on trying to access, for example,document.all.something; it's falsy becauseif (document.all)used to be a popular way to detect IE, before conditional comments. See Why is document.all falsy? for details
- This is a weird one.
"Falsey" simply means that JavaScript's internal ToBoolean function returns false. ToBoolean underlies !value, value ? ... : ...; and if (value). Here's its official specification (2020 working draft) (the only changes since the very first ECMAscript specification in 1997 are the addition of ES6's Symbols, which are always truthy, and BigInt, mentioned above:
Argument type Result Undefined Return false.Null Return false.Boolean Return argument. Number If argument is +0,-0, orNaN, returnfalse; otherwise returntrue.String If argument is the empty String(its length is zero), returnfalse; otherwise returntrue.BigInt If argument is 0n, returnfalse; otherwise returntrue.Symbol Return true.Object Return true.