Maybe you don't need to read all the article, i just can show you this:
undefined is a property of the global object.
It is a primitive value: undefined.
It is treated as falsy in boolean expressions.
undefined can be:
- the type of a variable that has not been assigned yet.
- the return value of a method or statement if the evaluated variable does not have an assigned value.
- the return value of a function, if no value was returned.
You can also explicitly set a variable to undefined: (don't do it)
const a = undefined; //accepted, but can lead to confusion!
null is an intentional absence of any object value.
It is a primitive value: null.
It is treated as falsy for boolean operations.
The value null is written with a literal: null.
null is not an identifier for a property of the global object.
Null expresses a lack of identification, indicating that a variable points to no object.
For Example, In many API, null is often retrieved in a place where an object can be expected but no object is relevant.
- null is an empty or non-existent value.
- null must be assigned.
Also, undefined and null are two distinct types:
- undefined is a type itself (undefined)
- unfortunately, null is of type object! (you can check this article to know more about it https://2ality.com/2013/10/typeof-null.html).
What do we get if we compare null and undefined with '==' and '===' operators?
console.log(null == undefined); // true console.log(null === undefined); // false console.log(typeof null); // "object" (not "null" for legacy reasons) console.log(typeof undefined); // "undefined"
Another difference is when we try to perform the arithmetic operation +
- with null results as an integer
- with undefined results is NaN
console.log(3 + null); //3 console.log(3 + undefined); //NaN
undefined typically means a variable has been declared, but not defined.
null is an assigned value, It means no value on purpose.
Top comments (5)
I think telling null is an object is confusing. Null is a primitive value and the only reason it pretends to be an object is a bug.
yes, it is just the return value of typeof(null), an old well known bug. Thanks for noticing it Logan, I will see if it can be said in a better way
That link helps, thank you!
I think you'd both find this conversation interesting. Sinder makes some valid points for eradicating
nulleven if some native APIs return
It's highly unlikely that
nullwill be leaving the language, but regardless, there are still interesting convos in that GitHub issue.