In most computer languages, there is only one value indicating & quot; no & quot;, for example, NULL in C, null in Java, none in Python, the nil in Ruby is a bit strange that the JavaScript language has two "NONE" values: undefined and null. Why?
1. Similarity
In JavaScript, assign a variable to undefined or null. To be honest, there is almost no difference.
The Code is as follows:
Var a = undefined;
Var a = null;
In the above Code, variables a are assigned undefined and null, which are almost equivalent.
Both undefined and null are automatically converted to false in the if statement. The Equal operator even directly reports that the two are equal.
The Code 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 code above shows how similar the two are!
Since the meanings and usage of undefined and null are similar, why do we need to set two such values at the same time? Doesn't it add JavaScript complexity without reason, and does it bother beginners? Dart, a replacement for JavaScript language developed by Google, makes it clear that there is only null, no undefined!
Ii. Historical Reasons
Recently, when I was reading the new book Speaking JavaScript, I accidentally found the answer to this question!
Originally, this was related to the history of JavaScript. When JavaScript was born in 1995, just like Java, only null was set as the value indicating "NONE.
According to the traditional C language, null is designed to be automatically converted to 0.
The Code is as follows:
Number (null)
// 0
5 + null
// 5
But Brendan Eich, the JavaScript designer, thinks this is not enough for two reasons.
First, null is treated as an object like in Java.
The Code is as follows:
Typeof null
// "Object"
However, JavaScript data types are divided into two categories: primitive and complex. Brendan Eich thinks that the value indicating "NONE" is better not an object.
Second, the original version of JavaScript does not include an error handling mechanism. When data types do not match, the type is automatically converted or fails silently. Brendan Eich thinks that if null is automatically converted to 0, it is not easy to find errors.
Therefore, Brendan Eich designed an undefined.
Iii. Initial Design
The original version of JavaScript is distinguished as follows: null is an object indicating "NONE", and the value is 0 when it is converted to a value; undefined is an original value indicating "NONE" and NaN when it is converted to a value.
The Code is as follows:
Number (undefined)
// NaN
5 + undefined
// NaN
Iv. Current usage
However, the above distinction will soon prove unfeasible in practice. Currently, null and undefined are basically synonymous, with only slight differences.
Null indicates "no object", that is, there should be no value. Typical usage:
(1) As a function parameter, it indicates that the function parameter is not an object.
(2) serves as the end point of the object prototype chain.
The Code is as follows:
Object. getPrototypeOf (Object. prototype)
// Null
Undefined indicates "Missing Value", that is, there should be a value, but it is not defined yet. Typical usage:
(1) The variable is declared, but when no value is assigned, it is equal to undefined.
(2) When calling a function, the required parameter is not provided. This parameter is equal to undefined.
(3) The object does not have an attribute assigned a value. The value of this attribute is undefined.
(4) When the function does not return a value, undefined is returned by default.
The Code is as follows:
Var I;
I // undefined
Function f (x) {console. log (x )}
F () // undefined
Var o = new Object ();
O. p // undefined
Var x = f ();
X // undefined