On the techniques of NULL and undefined_javascript in JavaScript

Source: Internet
Author: User

To say Null first, it represents a special value that is commonly used to describe "null values." Performs a typeof operation on NULL, and the result returns the string "Object", which means that null can be considered a special object value, meaning "Non-object" (It feels strange). In fact, it is generally assumed that NULL is the only member of its own type, and it can represent numbers, strings, and objects as "no value".

JavaScript also has a second value to indicate the value of the vacancy, that is, undefined, with undefined values to represent a deeper "null value." Undefined appears in 4 cases: ① variable declaration But no initialization ② the object property or array element does not exist ③ if the function does not have any return value, then the value of the Undefined④ reference function parameter that does not provide the argument is undefined only.

The two are the same: ① as mentioned earlier, they are all "false values", meaning that JavaScript expects to use a Boolean value, they are converted to False;② none of the two contain any properties and methods.

The difference: ①null is a keyword in the JavaScript language, and undefined is a predefined global variable for JavaScript, not a keyword. And, in ECMAScript 3, undefined is a readable, writable variable that can be assigned any value, and this error is corrected in ECMAScript 5. In this version of undefined is read-only (see the internet said that now the browser is basically supporting the ECMAScript 5, I do not know why I in the browser to assign a value of undefined no error, just did not change its value); ② performs typeof operations, NULL returns " Object string, undefined returns the "undefined" string.

As for comparing null with undefined, NULL = = undefined returns true,null = = undefined returns false. You can think of undefined as a vacancy for a system-level, unexpected, or error-like value, and Null is a vacancy representing a program-level, normal, or expected value. If you want to assign them to a variable or property, or pass in a function as a parameter, it is best to choose to use NULL.

The above mentioned is the entire content of this article, I hope you can enjoy.

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.