Distinguish and compare null and undefined in javascript, nullundefined

Source: Internet
Author: User

Distinguish and compare null and undefined in javascript, nullundefined

Differentiation and comparison of null and undefined in javascript

Undefined type

The Undefined type has only one value, that is, the special undefined. When var is used to declare a variable but it is not initialized, the value of this variable is undefined,

For example:

var message;alert(message == undefined); //true

This example only declares the variable message, but does not initialize it. Compare this variable with the undefined literal, and the result shows that they are equal. This example is equivalent to the following example:

var message = undefined;alert(message == undefined); //true

In this example, the undefined value is used to explicitly initialize the variable message. However, we do not need to do this because undefined values are obtained by default without initialization.

Generally, there is no need to explicitly set a variable to an undefined value. The primary purpose of the literal undefined is to compare it, which is not specified in versions earlier than ECMA-262 3rd. Version 3rd introduces this value to formally distinguish between null object pointers and uninitialized variables.

However, variables that contain undefined values are different from those that are not yet defined. Let's take a look at the example below:

Var message; // after this variable is declared, the undefined value is obtained by default. // The variable below is not declared // var age alert (message ); // "undefined" alert (age); // Error

Run the preceding code. The first warning box displays the value of the variable message, that is, "undefined ". The second warning box, because the variable age passed to the alert () function is not declared, will cause an error. For a variable that has not been declared, only one operation can be performed, that is, the typeof operator is used to detect its data type (calling delete For an undeclared variable will not cause errors, but this does not have any practical significance, in strict mode, errors occur ).

However, it is confusing that executing the typeof operator on uninitialized variables will return the undefined value, and executing the typeof operator on undeclared variables will also return the undefined value. Let's look at the following example:

Var message; // after this variable is declared, the undefined value is obtained by default. // The variable below is not declared // var agealert (typeof message ); // "undefined" alert (typeof age); // "undefined"

The result shows that the undefined value is returned for the uninitialized and undeclared variables when the typeof operator is executed. This result is logically reasonable. Although these two variables are essentially different from the technical point of view, in fact, no matter which variable is used, it is impossible to perform real operations.

Tip:

Even if uninitialized variables are automatically assigned undefined values, it is wise to initialize variables explicitly. If this can be done, when the typeof operator returns the "undefined" value, we will know that the detected variable has not been declared, rather than not yet initialized.

Null type

Null is the second data type with only one value. This special value is null. Logically, the null value indicates a null object Pointer, which is exactly why "object" is returned when the typeof operator is used to detect the null value, as shown in the following example:

 var car = null; alert(typeof car);  // "object"

If the defined variable is to be used to save the object in the future, it is better to initialize the variable to null instead of other values. In this way, you only need to check the null value to know whether the corresponding variable has saved an object reference, as shown in the following example:

If (car! = Null) {// perform some operations on the car object} In fact, the undefined value is derived from the null value, so the ECMA-262 requires that true be returned for their equality test: alert (null = undefined); // true

Here, the equal operator (=) between null and undefined always returns true, but note that this operator will convert its operands for the purpose of comparison, although null and undefined have such a relationship, their usage is completely different. As mentioned above, no variable value is explicitly set to undefined under any circumstances, but the same rule is not applicable to null. In other words, as long as the variable to save the object has not actually saved the object, it should be explicitly made to save the null value of the variable. This will not only reflect the Convention that null acts as a null Object Pointer, but also help to further differentiate null and undefined.

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.