JavaScript: undefined And null difference

Source: Internet
Author: User

JavaScript: undefined And null difference

When I was off duty, my colleague accidentally asked a question: What is the difference between undefined and null? I couldn't answer the question. I went back to check the relevant information. I had an understanding and made a summary. Before I started, please read the following code to throw this question:

console.info(undefined == null);    //trueconsole.info(undefined === null);   //false

The above results give us an understanding that the undefined and null values are equal, but they have different meanings. What are the differences between the two? First, let's take a look at undefined and null for analysis.

Undefined

The undefined type has only one value, that is, the special undefined. When a variable is declared with var but not initialized, the value of this variable is undefined. For example:

// 1. Declare the variable. The value var var1; console.info (typeof varl) is not specified; // undefined // 2. the specified value is undefinedvar var2 = undefined; console.info (typeof var2 ); // undefined

The preceding two statements are equivalent. undefined values are obtained by default without initialization.

Tips: In general, you do not need to display a variable to undefined. The main purpose of undefined is to compare it. The value introduced in the third edition of ECMA is to distinguish null and uninitialized variables of null object pointers.

Now that we have talked about null, we will add one more point here. If we execute typeof for the initialization variable, the undefined value will be returned. If we execute typeof for the declared variable, the undefined value will also be returned, as shown in the following example:

var var3;console.info(typeof var3);      //undefinedconsole.info(typeof var4);      //undefined

This result is logically rational. Although null and undefined are essentially different in terms of technology, it is impossible to operate on the two variables. In this case, if we are used to declaring variables for initialization, when the typeof operator returns the "undefined" value, we will know that some variables have not been declared, rather than not yet initialized.

Null

The null type also has only one null value. Logically, the null value indicates a null Object Pointer. Do not believe it? You can see the following code:

var var5 = null;console.info(typeof var5);      //object

Therefore, when a variable is used to save an object, it is initialized to null, so that you can easily know whether the variable saves the reference of an object.

Differences

I have understood these two types separately, but it seems I have understood them here, but they are not specific. Return to the problem thrown at the beginning. In the original type of ECMAScript, there are undefined and null types, which correspond to their own unique values undefined and null respectively. Undefined is actually derived from null, which can be interpreted as undefined = null is true.

Undefined = null is false:

Memory

Obviously, their address allocation is different. I think it is easy for people with backend basics to understand, for example:

Purpose

Although undefined = null is true, their usage is different. As described above, no variable value is displayed as undefined in any situation. The default value is undefined, but the default rule is not applicable to null. Null indicates that there is no reference to the object.

In general, undefined is derived from null. They are "=". On the other layer, undefined indicates the basic data type without a value assignment, and null indicates the reference data type without a value assignment, they cannot be "= ".


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.