Analysis of null and undefined judgments and differences in JavaScript

Source: Internet
Author: User
Tags reserved


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




The following are not the correct methods:


The code is as follows
var exp = null;
if (exp = null)
{
Alert ("is null");
}



When exp is undefined, it will also get the same result as NULL, although null and undefined are different. Note: This method can be used to determine both null and undefined.


The code is as follows
var exp = null;
if (!EXP)
{
Alert ("is null");
}



If exp is undefined, or the number zero, or false, the result will be the same as NULL, although NULL is not the same as the two. Note: This method can be used to determine both null, undefined, number 0, and false.


The code is as follows
var exp = null;
if (typeof exp = "NULL")
{
Alert ("is null");
}



For backward compatibility, when EXP is null, typeof null always returns object, so this cannot be judged.


The code is as follows
var exp = null;
if (IsNull (exp))
{
Alert ("is null");
}



There is IsNull this function in VBScript, but not in JavaScript.




--------------------------------------------------------------------------------



The following are the correct methods:


The code is as follows
var exp = null;
if (!exp && typeof exp!= "undefined" && exp!= 0)
{
Alert ("is null");
}
typeof exp!= "undefined" excludes undefined;
Exp!= 0 excludes the number zero and false.



The simpler and the right way:


The code is as follows

var exp = null;
if (exp = = NULL)
{
Alert ("is null");
}


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:


The code is as follows
Alert (' Undefined ' in window); Output: True
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 javascript reserved keywords!



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:


The code is as follows

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.



The following are not the correct methods:


The code is as follows

var exp = undefined;
if (exp = = undefined)
{
Alert ("undefined");
}
When exp is null, it also gets the same result as undefined, although null and undefined are different. Note: This method can be used to determine both undefined and null.

var exp = undefined;
if (typeof exp = = undefined)
{
Alert ("undefined");
}


typeof returns a string of six possible types: number, String, Boolean, object, function, undefined.




The following are the correct methods:


The code is as follows
var exp = undefined;
if (typeof exp = "undefined")
{
Alert ("undefined");
}


The following are the simpler and more correct methods (if the variable is neither Var nor assigned, the following error occurs, but the programmer, not JavaScript) is being reviewed:


  code is as follows

var exp = Undefined
if (exp = = undefined)
{
    alert ("undefined");
}

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.