In JavaScript development, I was asked: What is the difference between null and undefined? It is hard to answer at a time, especially undefined, because this involves the implementation principle of undefined. In JavaScript development, I was asked: What is the difference between null and undefined?
It is hard to answer at a time, especially undefined, because it involves the implementation principle of undefined. So, after thinking about it, write down this article. Please take a look.
Total offices: null = undefined
But: null! = Undefined
So what is the difference between the two?
Please listen to me again...
Null
This is an object, but it is null. Because it is an object, typeof null returns 'object '.
Null is the reserved keyword of JavaScript.
When a null value is involved in a numeric operation, its value is automatically converted to 0. Therefore, the following expressions obtain the correct value after calculation:
Expression: 123 + null result value: 123
Expression: 123 * null result value: 0
Undefined
Undefined is a special attribute of a Global Object (window). Its value is undefined. However, typeof undefined returns 'undefined '.
Although undefined has a special meaning, it is indeed an attribute and a Global Object (window) attribute. See the following code:
Alert ('undefined' in window); // output: true
Alert (undefined in window); // output: true
Var anObj = {};
Alert ('undefined' in anObj); // output: false
It can be seen that undefined is an attribute of the window object, but it is not an attribute of the anObj object.
Note: Although undefined is an attribute with special meanings, it is not a reserved keyword of JavaScript.
When undefined participates in any numerical calculation, the result must be NaN.
Just put, NaN is another special attribute of the Global Object (window), and Infinity is also. These special attributes are not reserved keywords of JavaScript!
Improve undefined Performance
When we use the undefined value in the program, we actually use the undefined attribute of the window object.
Similarly, when we define a variable but do not assign its initial value, for example:
Var aValue;
In this case, JavaScript sets its initial value as a reference to the window. undefined attribute during the so-called pre-compilation,
Therefore, when we compare a variable or value with undefined, it is actually compared with the undefined attribute of the window object. During this comparison, JavaScript searches for the properties of the window object named 'undefined', and then compares whether the reference pointers of the two operands are the same.
Because the window object has many attribute values, it takes time to search for the undefined attribute of the window object in each comparison with undefined. This may be a performance issue in functions that need to be frequently compared with undefined. Therefore, in this case, we can define a local undefined variable to speed up the comparison of undefined. For example:
Function anyFunc ()
{
Var undefined; // custom local undefined variable
If (x = undefined) // compare references in the scope
While (y! = Undefined) // reference comparison on the Scope
};
When undefined local variables are defined, the initial value is a reference to the window. undefined attribute value. The newly defined local undefined variable exists in the scope of the function. In subsequent comparison operations, the writing method of JavaScript code has not changed, but the comparison speed is fast. Because the number of variables in the scope will be much less than the properties of the window object, the speed of searching for variables will be greatly improved.
This is why many front-end JS frameworks often need to define a local undefined variable!