null and undefined in JS

Source: Internet
Author: User

Most programming languages typically have a value that says "None", while JS has two, null, and undefined. So I looked up some information and summarized it here for inspection.

There are two major classes of variables in JS, basic value types, and reference types. Where the value type is divided into: undefined,null,number,bolean,string five kinds. Visible null and undefined are different types.

a) undefined

The appearance of undefined generally has the following situations:

1 variable declaration but not initialized.

The property of the 2 object is not assigned a value.

3 When a function is called, the default parameter is undefined.

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

As can be seen from the above, when a variable or property is not initialized, the default value is undefined, so you do not need to display the setting of a variable with a value of undefined. At this point, you will want to do not need to display settings, why should he??? In fact it's just to differentiate between null object pointers (NULL) and uninitialized variables.

b) NULL

Null represents an empty object pointer, which stores an object that does not already exist. So this is the reason why you return "object" when you use the typeof operator to detect null. (typeof returns a string and the first letter is lowercase)

It is necessary to set the value to NULL if it is not necessary to display the setting variable value to undefined, and if the declared variable is used to represent the object and is not actually saved, it should be set to null.

Also, undefined is derived from null, so the return is true when "= =" is used.

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.