Most computer languages have and have only one value that represents "none", for example, the None,ruby language of the Null,python language of the C language Null,java language nil
Oddly enough, the JavaScript language actually has two values that represent "none": Undefined and null. Why is that? Similarity in JavaScript, assigning a variable to undefined or null, to be honest, is almost indistinguishable. Code is as follows: var a = undefined; var a = null; The above code, the A variable is assigned to undefined and null, the two are almost equivalent 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. Code 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 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 recently, I read the new book "Speaking JavaScript", accidentally found the answer to this question! 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. Code as follows: Number (NULL)//0 5 + null//5 However, JavaScript designer Brendan Eich that this is not enough, for two reasons. First, NULL is treated as an object, as in Java. Code as follows: typeof null//"Object" However, JavascripThe data type of T is divided into two categories, the original type (primitive) and the composite type (complex), and 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. The original version of JavaScript originally designed is this distinction: null is an object that represents "none", and when converted to a value, 0;undefined is an original value that represents "none", and Nan when converted to a value. Code as follows: Number (undefined)//Nan 5 + undefined//nan IV, current usage However, the above distinction is quickly 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. A typical use is: (1) as a function parameter, which means that the parameter of the function is not an object. (2) As the end point of the object prototype chain. Code as follows: Object.getprototypeof (object.prototype)//null undefined means "missing value", there should be a value here, but not yet defined. The typical usage is: (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. Code 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