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.
Falsey values in JavaScript
false
0
and other forms of numeric zero like-0
,0.0
and0x0
(credit to RBT for hex form)""
,''
and``
- strings of length 0null
undefined
NaN
document.all
(in HTML browsers only)document.all
is a falsey object, withtypeof
asundefined
. 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"Falsey" simply means that JavaScript's internal
ToBoolean
function returnsfalse
.ToBoolean
underlies!value
,value ? ... : ...;
andif (value)
. Here's its official specification (2018 working draft) (the only change since the very first ECMAscript specification in 1997 is the addition of ES6's Symbols, which are always truthy):Comparisons with
==
(loose equality)It's worth talking about falsy values' loose comparisons with
==
, which usesToNumber()
and can cause some confusion due to the underlying differences. They effectively form three groups:false, 0, -0, "", ''
all match each other with==
false == ""
,'' == 0
and therefore4/2 - 2 == 'some string'.slice(11);
null, undefined
match with==
null == undefined
butundefined != false
typeof null
returns'object'
,null
is not an object, this is a longstanding bug/quirk that was not fixed in order to maintain compatibility. It's not a true object, and objects are truthy (except for that "wilful violation"document.all
when Javascript is implemented in HTML)NaN
doesn't match anything, with==
or===
, not even itselfNaN != NaN
,NaN !== NaN
,NaN != false
,NaN != null
With "strict equality" (
===
), there are no such groupings. Onlyfalse
===
false
.This is one of the reasons why many developers and many style guides (e.g. standardjs) prefer
===
and almost never use==
.Truthy values that actually
== false
"Truthy" simply means that JavaScript's internal
ToBoolean
function returnstrue
. A quirk of Javascript to be aware of (and another good reason to prefer===
over==
): it is possible for a value to be truthy (ToBoolean
returnstrue
), but also== false
.You might think
if (value && value == false) alert('Huh?')
is a logical impossibility that couldn't happen, but it will, for:"0"
and'0'
- they're non-empty strings, which are truthy, but Javascript's==
matches numbers with equivalent strings (e.g.42 == "42"
). Since0 == false
, if"0" == 0
,"0" == false
.new Number(0)
andnew Boolean(false)
- they're objects, which are truthy, but==
sees their values, which== false
.0 .toExponential();
- an object with a numerical value equivalent to0
[]
,[[]]
and[0]
(thanks cloudfeet for the JavaScript Equality Table link)Some more truthy values
These are just a few values that some people might expect to be falsey, but are actually truthy.
-1
and all non-zero negative numbers' '
," "
,"false"
,'null'
... all non-empty strings, including strings that are just whitespaceAnything from
typeof
, which always returns a non-empty string, for example:typeof null
(returns a string'object'
due to a longstanding bug/quirk)typeof undefined
(returns a string'undefined'
)Any object (except that "wilful violation"
document.all
in browsers; remember thatnull
isn't really an object despitetypeof
suggesting otherwise). Including:{}
[]
function(){}
or() => {}
(any function, including empty functions)Error
and any instance ofError
new
(includingnew Number(0)
andnew Boolean(false)
)true
,1
,"1"
and[1]
returntrue
when compared to each other with==
.Just to add to @user568458's list of falsy values:
In addition to integer number 0, the decimal number 0.0, 0.00 or any such zeroish number is also a falsy value.
Above code snippet prints
I am a falsy value
Similarly hex representation of the number 0 is also a falsy value as shown in below code snippet:
Above code snippet again prints
I am a falsy value
.Don't forget about the non-empty string
"false"
which evaluates totrue