null and undefined in JS

Source: Internet
Author: User

The undefined type has only one value, which is undefined. When the declared variable is not initialized, the default value of the variable is undefined.
The null type also has only one value, which is null. Null is used to represent an empty object pointer. Null is associated with an object reference, which represents an object reference that is either empty or nonexistent.

var A;

Alert (Null = = a); True

Alert (Null = = undefined); True

ECMAScript that undefined are derived from null, so they are defined as equal. However, if in some cases we must differentiate between these two values, you can use the following two methods.

Alert (Null = = = undefined); False

alert (typeof null = = typeof undefined); False


The type of undefined;null that is undefined with the TypeOf method is a different type of object,null and undefined, so the output is "false". and = = = Absolute equals, where null = = = undefined output false.

There are several cases where the value of the variable is undefined
1, variable definition, but not assigned value, the value of the variable is undefined
2, the calling method does not return a value, the value returned is undefined
3, the property value of the object does not exist, the return value is undefined, such as: DOCUMENT.D

When NULL participates in numeric operations, its value is automatically converted to 0, so the following expressions are evaluated to get the correct values:
Expression: 123 + null result value: 123
Expression: 123 * Null result value: 0

When undefined participates in any numerical calculation, the result must be Nan.

null and undefined in JS

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.