The difference between undefined and null
Most computer languages have one and only one value representing “none”, such as NULL in C language, null in Java language, None in Python language, and nil in Ruby language.
It is a bit strange that the JavaScript language actually has two values that mean “none”: undefined and null. why is that?
1. Similarity
In JavaScript, assigning a variable to undefined or null is honestly the same.
var a = undefined; var a = null;
In the above code, the variable a is assigned to undefined and null respectively, and these two writing methods are almost equivalent.
Both undefined and null are automatically converted to false in the if statement, and the equality operator even directly reports that the two are equal.
if (!undefined) console.log('undefined is false'); // undefined is false if (!null) console.log('null is false'); // null is false undefined == null // true
The above code shows how similar the behavior of the two is!
Since the meanings and usages of undefined and null are similar, why set two such values at the same time? Isn’t this an increase in the complexity of JavaScript for no reason, and trouble beginners? The Dart language, an alternative to the JavaScript language developed by Google, clearly stipulates that there is only null and no undefined!
2. Historical reasons
Recently, when I was reading the new book “Speaking JavaScript” , I accidentally found the answer to this question!
It turns out that this has something to do with the history of JavaScript. When JavaScript was born in 1995 , just like Java, it only set null as the value representing “none”.
According to the tradition of the C language, null is designed to be automatically converted to 0.
Number(null) // 0 5 + null // 5
However, JavaScript designer Brendan Eich felt that this was not enough for two reasons.
First of all, null is treated as an object like in Java. However, JavaScript data types are divided into two categories: primitive and complex. Brendan Eich feels that the value of “none” is best not an object.
Secondly, the initial version of JavaScript did not include an error handling mechanism. When a data type mismatch occurs, the type is often automatically converted or silently fails. Brendan Eich feels that if null is automatically converted to 0, it is not easy to find errors.
Therefore, Brendan Eich designed another undefined.
Third, the initial design
The original version of JavaScript distinguished as follows: null is an object that represents “none” and is 0 when converted to a value; undefined is an original value that represents “none” and is NaN when converted to a value.
Number(undefined) // NaN 5 + undefined // NaN
Fourth, the current usage
However, the above distinction quickly proved infeasible in practice. Currently, null and undefined are basically synonymous, with only some subtle differences.
Null means “no object”, that is, there should be no value. Typical usage is:
(1) As a function parameter, it means that the function parameter is not an object.
(2) As the end of the object prototype chain.
Object.getPrototypeOf(Object.prototype) // null
undefined means “missing value”, that is, there should be a value here, but it has not been defined yet. Typical usage is:
(1) When the variable is declared but not assigned, it is equal to undefined.
(2) When calling the function, the parameter that should be provided is not provided, and the parameter is equal to undefined.
(3) The object has no attribute assigned, and the value of the attribute is undefined.
(4) When the function has no return value, it returns undefined by default.
var i; i // undefined function f(x){console.log(x)} f() // undefined var o = new Object(); o.p // undefined var x = f(); x // undefined
(over)