I have learned Javascript for several months, but I still could not differentiate null
and undefined
. This may sound silly, I know. So, in this article, I decided to dive deep the differences between the two.
Before we start, we have to understand the 7 primitive data types:
And there are 6 common known falsy values in Javascript(full list), which are:
false
0
and -0
(Also 0n
and -0n
)string
values: “”
, ''
, ````null
(the absence of any value)undefined
NaN
— not a numberAs we can see, null
and undefined
are treated as falsy
for boolean operations. There is something I would like to discuss that are added into ES2021 (or ES12) that is related to null
and undefined
, which we would explore at the end of this article.
#primitive-values #javascript