In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "the difference between null and undefined in JavaScript". In daily operation, I believe that many people have doubts about the difference between null and undefined in JavaScript. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "the difference between null and undefined in JavaScript". Next, please follow the editor to study!
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 variables
If (x = = undefined) / / comparison of references on scope
Comparison of references on while (y! = undefined) / / 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.
At this point, the study of "the difference between null and undefined in JavaScript" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.