The 6 falsy values in Javascript
Think fast JS dev, if asked you what are the 6 values considered falsy (evaluates to false), do you think you'd be able to answer with every single one?
If not, lets get into it, and learn a little bit about the double bang operator(!!
) too !
When it comes to conditionals, javascript will use something call type coercion, or the implicit conversion of values from one data type to another, to evaluate whatever is in the conditional to either be either truthy (evaluates to true) or falsy (evaluates to false).
So without further ado, the 6 types considered falsy are:
- the
false
keyword - the
undefined
primitive value - the
null
primitive value - an empty string (
''
,""
) - the
NaN
global property - a number representing
0
, like0.0
,-0
, or0n
So what's truthy? Well thats simple, if it's not on the list above, its truthy! In other words, as long as it's not falsy, it's truthy.
For example, although an empty array([]
) or likewise an empty object ({}
) may at first seem to be false, since they are not on the list above, it is a fact that they are instead truthy!
A way I like to check the truthiness of a value is with the double-bang operator !!
(or double-negation operator works too)
You can test this out in your browsers console:
Well that's it. I hope this little lesson on js booleans helped some people out.
You can read more about me or my current projects on my website