Maybe you don't need to read all the article, i just can show you this:
undefined
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
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).
Unassigned variables are initialized by JavaScript with a default value of undefined.
JavaScript never sets a value to null, that must be done by the developer.
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"
Arithmetic Operations
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
in Conclusion
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)
The value 'undefined' denotes that a variable has been declared, but hasn't been assigned any value. On the other hand, Javascript null refers to a non-existent object, which basically means 'empty' or 'nothing'. They're both values usually used to indicate the absence of something . Also, undefined and null are two distinct types: undefined is a type itself (undefined) while null is an object .
net-informations.com/js/iq/nuvsun.htm
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
null
even if some native APIs returnnull
.It's highly unlikely that
null
will be leaving the language, but regardless, there are still interesting convos in that GitHub issue.