The difference between null and undefined in JavaScript _javascript skills

Source: Internet
Author: User

There are two original types in javascript: null and undefined. These two types often make JavaScript developers wonder when they are null and when are undefined?

The undefined type has only one value, that is, undefined. The default value for a variable is undefined when the declared variable has not yet been initialized.

A null type also has only one value, that is, null. Null is used to represent an object that does not exist, and is commonly used to represent a function attempt to return an object that does not exist.

Copy Code code as follows:

var Ovalue;
Alert (Ovalue = = undefined); Output "true"

This code appears to be true, representing the value of Ovlaue as undefined, because we did not initialize it.

Copy Code code as follows:

Alert (Null = = document.getElementById (' notexistelement '));

When a DOM node with ID "notexistelement" does not exist on the page, this code appears "true" because we are trying to get an object that does not exist.

Copy Code code as follows:

Alert (typeof undefined); Output "undefined"
Alert (typeof null); Output "Object"

The first line of code is easy to understand, the undefined type is undefined; the second line of code makes one wonder why the type of NULL is object again. In fact, this is the first implementation of JavaScript error, and later by ECMAScript. As we can explain today, NULL is a placeholder for an object that does not exist, but you should pay attention to this feature when you actually encode it.

Copy Code code as follows:

Alert (Null = = undefined); Output "true"

ECMAScript that undefined are derived from null, so they are defined as equal. However, if in some cases, we must distinguish between these two values, what should we do? You can use both of the following methods.
Copy Code code as follows:

alert (null = n = undefined); Output "false"
alert (typeof null = = typeof undefined); Output "false"

Using the TypeOf method, the null is not the same as the undefined type, so the output is "false". and = = = = Absolute equals, here null = = undefined output false.

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.