Sometimes when programming in JavaScript, it is easy to get an uncertain feeling of, what in JavaScript are falsy?
It is slightly different if it is pure JavaScript versus in a browser.
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...
and more...
The answer is, there were 5 primitive types in JavaScript ES5. Each primitive type contributes 1 falsy value, except Number, which contributes 3:
The way I remember them is:
NNBUS
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 n
is 0
or -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
The operator ===
would compare 0
and -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 true
for 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: 0n
.
Now, almost all objects in JavaScript are truthy values, except one. Do you know what it is? Together, they make up the 9 values in JavaScript that are falsy, and the object is 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
.
document.all // HTMLAllCollection(3) [html, head, body]
!!document.all // false
So the falsy values in JavaScript in a browser, 9 of them, are:
null
0
-0
NaN
false
undefined
''
0n
document.all
Top comments (10)
Is document.all really part of JS? I don’t think you can access it in node for example. So I would think it’s exposed via the browser api, and behavior on the browser itself.
But really great write up, thank you!
I guess just a familiar JS environment (some of us grew up with)... I think the amazing thing is, if the browser complies to JS, there is no way that it can make
document.all
to be falsy? If there are some weird things happening in NodeJS regarding JS I'd hope to know too.By the way... I updated the post to mention it is inside of a browser, thank you.
Good write-up!
One note though... Actually, technically speaking, and according to the JavaScript specs (ECMA 262), there are only 7 values that are Falsy, meaning that they are considered false when encountered in a Boolean context.
And when it comes to
document.all
it's not part of the JavaScript specs per se. It's part of the DOM API (W3C), which browser-based only (Node.js doesn't have a document object, nor a DOM).Cheers ;)
It is 7 but not 8? Which one is not listed... (I tried to search for "falsy" or "falsey" in that docs but can't find any match)... true,
document.all
really is inside of a browser only, so I think I will update the postYou can find more info here: developer.mozilla.org/en-US/docs/G...
oh... not counting
-0
... depending on how you look at it. It can be incorrect, because0
and-0
are technically different.What about
[]
,"\t"
and"\r"
, I think those values are falsey too!In Google Chrome or in NodeJS:
!![]
is the same asBoolean([])
In AngularJS some time ago, I remember AngularJS would consider
[]
to be falsy but in a later version to be truthy: stackoverflow.com/questions/357763...Nice!
Why
document.all
is falsy - stackoverflow.com/questions/103501...