Undefined and null
Most computer languages have one and only one value for “None”, such as NULL for C, NULL for Java, None for Python, and nil for Ruby.
Oddly enough, the JavaScript language has two values for “none” : undefined and null. Why is that?
I. Similarity
In JavaScript, assigning a variable to undefined or null makes almost no difference.
var a = undefined; var b = null; a==b//true a===b//true Copy the code
In the code above, the a variable is assigned to undefined and null, respectively, which are almost equivalent.
Undefined and null are automatically converted to false in if statements, and the equality operator even reports that they are equal.
if (! undefined) console.log('undefined is false'); // undefined is false if (! null) console.log('null is false'); // null is false undefined == null // trueCopy the code
The code above shows how similar the two behave!
Since undefined and null have the same meaning and usage, why set both values at the same time? Dart, Google’s alternative JavaScript language, specifies null and undefined.
Second, historical reasons
I recently stumbled upon the answer to this question while reading my new book Speaking JavaScript!
Turns out, it has to do with the history of JavaScript. When JavaScript was born in 1995, it initially set only null as a value for “none”, just like Java.
In the tradition of C, NULL is designed to be automatically converted to 0.
Number(null) // 0 5 + null // 5 Copy the code
But Brendan Eich, the designer of JavaScript, feels that this is not enough, for two reasons.
First, NULL is treated as an object, just like in Java. However, JavaScript data types fall into primitive and complex types, and Brendan Eich felt that a value for “nothing” is best not an object.
Second, the earliest versions of JavaScript did not include error handling, and when a data type mismatch occurred, the type was automatically converted or silently failed. Brendan Eich felt that if null was automatically converted to 0, it would be hard to spot errors.
So Brendan Eich designed another undefined.
3. Initial design
The original version of JavaScript made this distinction: NULL is an object representing “nothing” that is zero when converted to a value; Undefined is a primitive value for “none”, which is NaN when converted to a value.
Number(undefined) // NaN 5 + undefined // NaN Copy the code
Iv. Current usage
However, such distinctions soon proved unworkable in practice. Currently, null and undefined are basically synonymous with each other, with some subtle differences.
Null means “no object”, meaning there should be no value. Typical usage:
(1) as the parameter of the function, indicating that the parameter of the function is not an object.
(2) as the end of the object prototype chain.
Object.getPrototypeOf(Object.prototype) // null Copy the code
Undefined means “missing value”, that is, there should be a value here, but it is not defined yet. Typical usage:
(1) If a variable is declared but not assigned, it is undefined.
(2) When the function is called, the argument that should be provided is not provided, which is equal to undefined.
(3) The object has no assigned attribute. The value of this attribute is undefined.
(4) If the function does not return a value, undefined is returned 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 Copy the code