Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between undefined and null in JS foundation

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about the difference between undefined and null in the foundation of JS. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

In JavaScript development, people were asked: what is the difference between null and undefined?

It is difficult to answer for a while, especially undefined, because it involves the implementation principle of undefined.

It is well known that:

Null = = undefined

But:

Null! = = undefined

So what's the difference between the two?

Null

This is an object, but empty. Because it is an object, typeof null returns' object'.

Null is the JavaScript reserved keyword.

When null participates in a numeric operation, its value is automatically converted to 0, so the following expression evaluates to the correct value:

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 returns' undefined'.

Although undefined has a special meaning, it is indeed a property and a property of the global object (window). Take a look at the following code:

Alert ('undefined' in window); / / output: true var anObj = {}; alert (' undefined' in anObj); / / output: false

You can see that undefined is a property of the window object, but not a property of the anObj object.

Note: although undefined is an attribute with a special meaning, it is not a reserved keyword for JavaScript.

When undefined participates in any numerical calculation, the result must be NaN.

By the way, NaN is another special property of the global object (window), and so is Infinity. None of these special properties are reserved keywords for JavaScript!

Improve undefined performanc

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 without giving it an initial value, for example:

Var aValue

At this point, JavaScript sets its initial value to a reference to the window.undefined property during the so-called precompilation

So when we compare a variable or value to undefined, we are actually comparing it to the undefined property of the window object. During this comparison, JavaScript searches for the property of the window object named 'undefined', and then compares the reference pointers of the two operands to see if they are the same.

Because the window object has so many property values, it takes time to search for the undefined property of the window object every time it is compared to undefined. This can be a performance problem in functions that require frequent comparisons 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) / / reference comparison on scope while (y! = undefined) / / reference comparison on scope}

Where, when a undefined local variable is defined, its initial value is a reference to the value of the window.undefined attribute. The newly defined local undefined variable exists in the scope of the function. In the subsequent comparison operation, there is no change in the way the JavaScript code is written, but the comparison is very fast. Because the number of variables in the scope is much smaller than the properties of the window object, the speed of searching for variables is greatly increased.

This is why many front-end JS frameworks often define their own local undefined variables!

The above is the difference between undefined and null in the JS foundation shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report