In JavaScript development, people ask: what is the difference between null and undefined? A bad time to answer, especially undefined, because it involves the implementation of undefined principle.
General known: null = = undefined
However: null!== undefined
So what's the difference between the two?
Null
This is an object, but it is empty. Because it is an object, typeof null returns ' object '.
Null is a JavaScript reserved keyword.
When NULL participates in numeric operations, its value is automatically converted to 0, so the following expressions are evaluated to get the correct values:
Expression: 123 + null result value: 123
Expression: 123 * Null result value: 0
Undefined
Undefined is a special property of the Global Object (window) whose value is undefined. But typeof undefined returned ' undefined '.
Although undefined has a special meaning, it is really a property and a property of the Global Object (window). Take a look at the following code:
1 |
alert( ‘undefined‘ in window); //输出:true |
3 |
alert( ‘undefined‘ in anObj); //输出:false |
As you can see, undefined is a property of the Window object, but it is not a property of the Anobj object.
Note: Although undefined is a special-meaning property, it is not a reserved keyword for JavaScript.
When undefined participates in any numerical calculation, the result must be Nan.
To say the same, Nan is another special property of the Global Object (window), and so is infinity. These special properties are not javascript reserved keywords.
Improved undefined performance
When we use the undefined value in our program, we are actually using the undefined property of the Window object.
Similarly, when we define a variable but do not give it its initial value, for example:
var avalue;
At this point, JavaScript sets its initial value to a reference to the Window.undefined property at the time of the so-called precompilation.
Thus, when we compare a variable or value to undefined, it is actually compared to the undefined property of the Window object. In this comparison, JavaScript searches the properties of the window object called ' undefined ', and then compares the reference pointers for the two operands.
Because of the very many property values of the Window object, it takes time to search the undefined property of the Window object for each comparison with undefined. In a function that requires frequent comparisons with undefined, this can be a performance problem point. Therefore, in this case, we can define a local undefined variable, to speed up the comparison of undefined. For example:
03 |
var undefined; //自定义局部undefined变量 |
05 |
if (x == undefined) //作用域上的引用比较 |
08 |
while (y != undefined) //作用域上的引用比较 |
Where a undefined local variable is defined, its initial value is a reference to the value of the Window.undefined property. The newly defined local undefined variable exists on the scope of the function. There was no change in the way JavaScript code was written in the subsequent comparison operation, but it was relatively fast. Because the number of variables on the scope is much less than the properties of the Window object, the speed of the search variable is greatly improved.
That's why many front-end JS frameworks often have to define a local undefined variable for themselves.
The difference between null and undefined in JS