Most computer languages have and have only one value that denotes "none", such as the nil of the None,ruby language of the Null,python language of the C language Null,java language.
Oddly enough, the JavaScript language actually has two values that represent "none": Undefined and null. Why is that?
First, similarity
In JavaScript, assigning a variable to undefined or null is, to be honest, almost indistinguishable.
var a = undefined;
var a = null;
In the code above, the A variable is assigned to undefined and null, and the two are almost equal to each other.
Undefined and null are automatically converted to false in an if statement, and the equality operator even reports directly to the same.
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 behavior is!
Since undefined and null are similar in meaning to usage, why should you set two such values at the same time, this is not an unwarranted increase in the complexity of JavaScript, so that beginners bothered? Google has developed a JavaScript language alternative to the Dart language, which explicitly stipulates that only null, no undefined!
Second, historical reasons
I recently discovered the answer to this question when I was reading the book Speaking JavaScript!
Originally, this is related to the history of JavaScript. When JavaScript was born in 1995, it was originally like Java, setting only null as a value that represented "none."
According to the C language tradition, NULL is designed to be automatically converted to 0.
Number (NULL)
//0
5 + null
/5
But Brendan Eich, a JavaScript designer, feels it's not enough, for two reasons.
First, NULL is treated as an object, as in Java. However, the data type of JavaScript is divided into two categories, the original type (primitive) and the composite type (complex), Brendan Eich feel that the value representing "None" is best not an object.
Second, the original version of JavaScript did not include the error handling mechanism, when the data type mismatch occurred, often the automatic conversion type or silently failed. Brendan eich that if NULL automatically to 0, it is not easy to find errors.
Therefore, Brendan Eich also designed a undefined.
Third, the original design
The original version of JavaScript is distinguished by this: null is an object that represents "none", and when converted to a value, 0;undefined is an original value representing "None", which is Nan when converted to a value.
Number (undefined)
//Nan
5 + undefined
//Nan
Iv. Current Usage
However, the above distinction is soon proved to be not practical in practice. At present, null and undefined are basically synonymous, with only a few subtle differences.
Null means "No object", where there should be no value. Typical uses are:
(1) as a parameter of the function, which indicates that the parameter of the function is not an object.
(2) As the end point of the object prototype chain.
Object.getprototypeof (Object.prototype)
//null
Undefined means "missing value," where there should be a value, but not yet defined. Typical uses are:
(1) When a variable is declared, it is equal to undefined when it is not assigned a value.
(2) when calling a function, the supplied argument is not supplied, which equals undefined.
(3) The object has no assigned property, and the value of the property is undefined.
(4) When the function does not return a value, the default returns undefined.
var i;
I//undefined
function f (x) {Console.log (x)}
F ()//undefined
var o = new Object ();
O.P//undefined
var x = f ();
X//undefined
Here's a little summary.
Null is a JavaScript keyword that represents a non object.
Undefined indicates no value, indicating that the value does not exist or is not initialized.
Same point |
Different points |
Own type unique value |
Null is an object, undefined is a reserved word |
No properties and methods |
The conversion of NULL into 0,undefined in mathematical operations cannot be converted or converted into Nan |
= = Think equal, = = = Think Unequal |
Null!==undefined//true |
are false values, the Boolean variable represents the same value |
Null is an empty object, undefined is a window's property (but not an object property) |
|
Alert (typeof null)//object; Alert (typeof undefined)//undefined; |