When it was ES5, there were 8 falsy values. Can you name them all?
Scroll down to read what they are...
and scroll some more...
The way I remember them is:
Null, Number, Boolean, Undefined, String
And the corresponding falsy values are:
null 0 -0 NaN false undefined ''
So this is "falsy 7". There is one more, at the end of this post.
0 can be viewed as the same as
-0 sometimes, depending on how you look at it. If you have
n dollars, and
-0, it really doesn't matter that much. But let's say, if you calculate something, and it is
1 / 0 or
1 / -0, then they give very different results:
1 / 0 // Infinity 1 / -0 // -Infinity
=== would compare
-0 to be the same, but
Object.is() would compare them as different:
0 === -0 // true Object.is(0, -0) // false
The other different behavior of
Object.is() is that it returns
Object.is(NaN, NaN), while
NaN === NaN returns false. So in a way, you can think of
Object.is() as the twin brother of
=== that is more precise.
You can also think of
0 not being equal to
-0: when it is
+0, a tiny bit of matter, it can cost you nothing, such as a grain of sand. But if it is
-0, which is a tiny bit of antimatter, it would cost you a lot of money, as the current price of 1 gram of antimatter is $62.5 trillion dollars.
In ES6, there are two more new primitive types: Symbol and BigInt. Symbol is the only primitive type that doesn't have a falsy value, and BigInt also contributes one falsy value:
document.all, and is falsy coming from the usage when the code checks
if (document.all) and
document.all is falsy to tell the program: don't use
document.all // HTMLAllCollection(3) [html, head, body] !!document.all // false
null 0 -0 NaN false undefined '' 0n document.all