You might be thinking that
undefined is the same. You have to know that, spoiler alert, they are not. What I mean by this is: yes both have many similarities, but deep down it's totally different.
It's confusing, I know. But you need to know these basic things. So let's jump!
Both of them usually called nullish value: have either
undefined value. These also listed as
falsy values, which are:
0(zero) —(also for
NaN(Not a Number)
Falsy values itself means that the value will be generated as false on the boolean approach.
From the definition,
null is an intentional absence of a value. While
undefined is a default value for a variable with no value assigned.
const bowl = null; //null const bowl; //undefined
As mentioned above, both are falsy values. Even so, it works this way:
null == undefined null !== undefined
null as an empty object, hence if you do
typeof null it will show
undefined is a primitive value. The type of
undefined will be
If we try to make equalization on a number format,
null will be identified as a 0 (zero), while
undefined will not be recognized as any number (
NaN). Like this:
const addNum = 8 + null // 8 const addNum = 8 + undefined // NaN
null have the same value, the same nullish and falsy, but each has a different type. Hence use in its own place.