Introduction to null, undefined, and nullundefined in JavaScript

Source: Internet
Author: User

Introduction to null, undefined, and nullundefined in JavaScript

Null indicates a special value, which is often used to describe "null ". Perform the typeof operation on null, and the result returns the string "object". That is to say, null can be considered as a special object value, meaning "non-object" (strange feeling ). In fact, null is generally considered to be the only member of its own type. It can indicate that numbers, strings, and objects are "valueless.

JavaScript also has a second value to indicate the vacancy of the value, that is, undefined. The undefined value is used to indicate a deeper "null ". When undefined is declared as a variable but not initialized, ② when the object attribute to be queried or the element of the array does not exist, ③ if the function does not return any value, then, return the undefined ④ value of the function parameter that does not provide the real parameter, and only get undefined.

Similarities between the two: ① as mentioned above, they are all "false values", that is, when JavaScript expects to use a Boolean value, they will be converted to false; ② neither of them contains any attributes or methods.

Differences between the two: ① null is a keyword of the JavaScript language, while undefined is a predefined global variable of JavaScript, not a keyword. Moreover, in ECMAScript 3, undefined is a readable and writable variable that can be assigned any value. This error is corrected in ECMAScript 5, in this version, undefined is read-only (I can see that the current browser basically supports ECMAScript 5 on the Internet. I don't know why I didn't report an error when assigning a value to undefined in the browser, only does not change its value); ② executes the typeof operation, null returns the "object" string, and undefined returns the "undefined" string.

For comparison between null and undefined, null = undefined returns true, null = undefined returns false. It can be considered that undefined represents a system-level, unexpected, or similar false value vacancy, while null represents a program-level, normal, or expected value vacancy. If you want to assign values to variables or attributes or input functions as parameters, it is best to use null.

The above is all the content of this article. I hope you will like it.

Related Article

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.