Javascript:undefined and Null differences

Source: Internet
Author: User

at dinner time, colleagues occasionally asked a question: what is the difference between undefined and null? Unable to answer, go back to consult the relevant information, forget to have a understanding, do the relevant summary, before starting, please look at the following code, is to throw this question:

Console.info (undefined = = null);    Trueconsole.info (undefined = = = null);   False

The above results can give us one such understanding: undefined and null two values are equal, but they have different meanings. What is the difference between the two, first understand the next undefined and null in doing analysis.

undefined

The undefined type has only one value, that is, a special undefined. When you declare a variable with VAR but do not initialize it, the value of this variable is undefined. For example:

1, declare variable, unspecified value var var1;console.info (typeof Varl);      UNDEFINED//2, the specified value is Undefinedvar var2 = Undefined;console.info (typeof var2);      Undefined

The above two formulations are equivalent, the uninitialized value will get the undefined value by default.

Tips: Generally no need to display the main purpose of setting a variable to undefined,undefined is to compare, the ECMA Third Edition introduces this value precisely to differentiate null object pointers and uninitialized variables

Now that we've talked about NULL, here's a little bit more to do with the TypeOf return undefined value for the initialized variable, and the undefined value for executing the typeof for the declared variable, as in the following example:

var var3;console.info (typeof Var3);      Undefinedconsole.info (typeof VAR4);      Undefined

This result has logical rationality, although the null and the undefined have the essential difference in the technical angle, but in reality it is impossible to manipulate the two kinds of variables. In this case, if we are accustomed to declaring variables to be initialized, when the typeof operator returns the "undefined" value, we know that a variable has not been declared, not yet initialized.

NULL

The null type is also a value of NULL, from a logical point of view, a null value represents an empty object pointer, does not believe? Look at the following code to know:

var var5 = Null;console.info (typeof var5);      Object

So when a variable is used to hold an object, it is finally initialized to NULL, which makes it easy to know if a variable holds a reference to an object.

difference

Understanding the two types separately, it seems to have some understanding, but not specific. Back to the problem originally thrown, in the original type of ECMAScript, there are undefined and null types, respectively, which correspond to their own unique values undefined and null. Undefined is actually derived from null, which can be interpreted as true for Undefined==null.

There are two ways to understand Undefined===null as false:

Memory

obviously, their address assignment is not the same, I think for the back-end base of the students are very easy to understand, specific such as:

Use

Although Undefined==null is true, their use is not the same. As mentioned earlier, there is no need to specify the value of a variable as undefined, and the default value is undefined, but the default rule does not apply to NULL. Because null means there is no reference to the object.

In general, undefined derive from null, they are "= =", and at the other level undefined represents a basic data type that has no assignment, and null represents a reference data type that does not have an assignment, and they cannot "= = =".


Javascript:undefined and Null differences

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.