The difference between null and undefined in JS

Source: Internet
Author: User

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‘inwindow);   //输出:true
2 varanObj = {};
3 alert(‘undefined‘inanObj);    //输出: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:

01 functionanyFunc()
02     {
03         varundefined;          //自定义局部undefined变量
04         
05         if(x == undefined)      //作用域上的引用比较
06         
07         
08         while(y != undefined)   //作用域上的引用比较
09         
10     };

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

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.