Talk about JavaScript null and undefined

Source: Internet
Author: User
Tags hasownproperty

As long as it is about JS variables and data types, you can not open null and undefined, the two brothers is the important basis of JS, it is necessary to observe, countless students have used a magnifying glass multi-angle multi-batch study of the brothers, is very popular. ^-^

JS is really weird, there is a null enough, but also to toss out a undefined.

Although it is criticized, but this is the design.

In C # and Java, null means NULL, and no memory is allocated.

Null in JS takes number (NULL) as the result of 0, which means that it is not empty, is simply null, there is no value, and its data type is "Object".

So to represent the empty, so the designer made a undefined, it has a separate data type is "undefined".

The result of using number (undefined) is Nan, and the result of the conversion can be achieved for design purposes.

As a result of this design, the conversion of data types in an expression plays a key role.

An undefined variable or a defined but unassigned variable represents an empty, or undefined, value.

Undefined more is the use of JavaScript itself.

For example, the code farm to delete an object's properties is not to assign this property to undefined, because this property is still in, with hasOwnProperty to detect this property or exist, where some logic needs to be judged by the problem, if you delete the attribute is the predecessor delete.

var o1={p1: ' v1 ', P2: ' v2 '};o1.p2=undefined;o1.hasownproperty (//true

To delete an array element, for no side effects, use splice.

If you use undefined to assign values, you need to think clearly.

For code farmers, more is the use of NULL, you can assign a variable above the value, break the contact with the object instance, to ensure garbage collection, or in case the variable symbol for subsequent use.

Null and undefined are also pit pits when compared.

NULL // true NULL false // false false // false

Although weird, but that's the result, that's the design.

You can view the relevant definitions for ECMA-262, the rule Definitions Link 1, link 2.

END

Talk about JavaScript null and undefined

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.