Undefined and null understanding

Source: Internet
Author: User

In JS, for a variable without a given initial value after life, a value of undefined is given. But the premise is that the variable must have been declared, if it is not declared variables, will be an error.

v1; // error, VI is not defined var // undefined var v2 = "";  //

Typical Uses of undefined:

1. The variable is declared, but no assignment is undefined;

2. When calling a function, the parameters that should be provided are not provided, then the default parameter is undefined;

3. There are no assigned attributes in the object, which is undefined;

4. If the function does not return a value, undefined is returned by default.

In contrast, NULL represents a special value of object that is used to represent the concept of a null reference. If you want an identifier to be declared as object, but you do not give him an instance temporarily, you can first initialize it to null for later use.

Typical Uses of NULL:

1. Null means no object, that is, there should be no value here;

2. As a function parameter, indicates that the function's parameters are not objects;

3. As the focus of the object prototype chain.

A very interesting statement to:

null;   // true  null;  // false

There is a very interesting statement on the Internet, of course, I also feel that this statement is very reasonable:

Undefined is derived from null: the base data type has a reference type corresponding to it. Just like number corresponds to number, Boolean corresponds to Boolean ... They have the same behavior, and each of them will have similar unpacking and boxing operations.

For the behavior of NULL and undefined:

1. Null participation in numeric operations is automatically converted to 0;

2. typeof Null returns an Object because null represents a reference with no value;

3. Undefined is a special property of the global object whose value is the undefined type's proprietary value undefined;

4. When undefined participates in any numerical operation, the result must be Nan;

  

Are the original notes on the finishing, some of the source has not been found, and so on later to see the source of time to make up, sorry ...

Undefined and null understanding

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.