Anatomy of NULL and Undefined__java in JavaScript

Source: Internet
Author: User
Tags reserved

In JavaScript development, people ask: what is the difference between null and undefined.

A bad time to answer, especially undefined, because this involves the realization of the principle of undefined. So, after thinking carefully, write down this article, please heroes to shoot bricks.

Total known: null = = undefined

However: null!== undefined

So what's the difference between the two?

Please listen to me explain ...

NULL

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

Null is a JavaScript reserved keyword.

Null is automatically converted to 0 when it participates in numeric operations, 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 returned to ' undefined '.

Although undefined has a special meaning, it is indeed a property and is a property of the Global Object (window). Take a look at the following code:
Code highlighting produced by Actipro Codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> alert (' Undefined ' in window); Output: True

var anobj = {};
Alert (' undefined ' in anobj); Output: 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 attribute, it is not a reserved keyword for JavaScript.

Undefined participates in any numerical calculation, the result must be Nan.

By the way, Nan is another special property of the Global Object (window), infinity. These special properties are not reserved keywords for javascript.

Improve undefined performance

When we use the undefined value in a program, we actually use the undefined property of the Window object.

Similarly, when we define a variable but do not give its initial value, for example:

var avalue;

At this point, JavaScript sets its initial value to a reference to the Window.undefined property when called precompiled.

So when we compare a variable or value to a undefined, it is actually compared to the undefined property of the Window object. During this comparison, JavaScript searches for the properties of the window object called ' undefined ', and then compares the reference pointers of the two operands to the same.

Because the property values of the Window object are very numerous, it takes time to search for the undefined property of the Window object in each comparison with the undefined. This can be a performance problem in a function that requires frequent comparisons with undefined. Therefore, in this case, we can define a local undefined variable to speed up the comparison of undefined. For example:
Code highlighting produced by Actipro Codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> function Anyfunc ()
{
var undefined; Custom Local undefined variables

if (x = = undefined)//reference comparison on scope


while (y!= undefined)//scope reference comparison

};

Where the undefined local variable is defined, its initial value is a reference to the Window.undefined property value. The newly defined local undefined variable exists on the scope of the function. In subsequent comparisons, the JavaScript code was written in a way that was not changed, but 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 can be greatly improved.

This is why many front-end JS frameworks often have to define a local undefined variable for themselves.

Original: Li Shi (Leadzen). Hangzhou-Ali Software 2009-2-18
In-situ: http://blog.csdn.net/leadzen/archive/2009/02/17/3899392.aspx
"Reprint please indicate the author and the source"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.