What's a bit strange is that the JavaScript language actually has two values that represent "non": undefined and null. why is that?
1. Similarity
In JavaScript, assigning a variable to undefined or null, honestly, almost no difference.
The code copy is as follows:
var a = undefined;
var a = null;
In the above code, the a variable is assigned to undefined and null respectively, and these two writing methods are almost equivalent.
Undefined and null are automatically converted to false in the if statement, and the equality operator even directly reports that the two are equal.
The code copy is as follows:
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 behaviors of the two are!
Since the meaning and usage of undefined and null are similar, why do you need to set two such values at the same time? Isn’t this unreasonable increase in the complexity of JavaScript and bothering beginners? Dart, a JavaScript alternative to 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 discovered the answer to this question!
It turns out that this is related to the history of JavaScript. When JavaScript was born in 1995, it was initially set like Java, only null was set as a value indicating "none".
According to the tradition of C language, null is designed to automatically turn to 0.
The code copy is as follows:
Number(null)
// 0
5 + null
// 5
However, JavaScript designer Brendan Eich thinks that this is not enough, for two reasons.
First, null is treated as an object like in Java.
The code copy is as follows:
typeof null
// "object"
However, JavaScript's data types are divided into two categories: primitive and synthetic types (complex). Brendan Eich thinks that the value that means "none" is best not an object.
Secondly, the initial version of JavaScript did not include an error handling mechanism. When data type mismatch occurs, it often automatically converts the type or fails silently. Brendan Eich thinks that if null automatically turns to 0, it is not easy to find errors.
Therefore, Brendan Eich designed another undefined.
3. Initial design
The original version of JavaScript distinguished it like this: null is an object that represents "none", which is 0 when converted to a numeric value; undefined is an original value that represents "none", which is NaN when converted to a numeric value.
The code copy is as follows:
Number(undefined)
// NaN
5 + undefined
// NaN
4. Current usage
However, the above distinction was quickly proven unfeasible in practice. At present, null and undefined are basically synonymous with only some slight differences.
null means "no object", that is, there should not be a value there. Typical usage is:
(1) As a parameter of a function, the parameter of the function is not an object.
(2) As the end point of the object prototype chain.
The code copy is as follows:
Object.getPrototypeOf(Object.prototype)
// null
undefined means "missing value", which means there should be a value here, but it has not been defined yet. Typical usage is:
(1) When a variable is declared but has no assignment, it is equivalent 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 to the value, and the value of the attribute is undefined.
(4) When the function does not return a value, undefined is returned by default.
The code copy is as follows:
var i;
i // undefined
function f(x){console.log(x)}
f() // undefined
var o = new Object();
op // undefined
var x = f();
x // undefined