null and undefined

Source: Internet
Author: User

Speaking of null and undefined, I believe many people are both familiar and confused.

Null is a special value that is commonly used to describe "null", which can represent numbers, strings and objects as "no value", and null execution typeof will get "object".

Undefined is a value of the variable, indicating that the variable is not initialized, and undefined execution typeof will be "undefined".

Because both represent "empty values", the equality operator "= =" is judged to be equal, and the result is true, which requires "= = =" To distinguish between the two, which returns false at that time.

Null should be used in the following scenario:

1. Used to initialize a variable, this variable may be assigned to an object;

2. To compare with an already initialized variable, this variable can or may not be an object;

3. When the function parameter is expected to be an object, it is used as a parameter;

4. When the return value of a function is expected to be an object, it is used as the return value.

Null should not be used in the following scenario:

1. Do not use NULL to detect whether a parameter is passed in;

2. Do not use NULL to detect an uninitialized variable.

The best way to understand null is to treat it as a placeholder for the object.

Those variables that are not initialized have an initial value of "undefined", which means that the variable waits for the value to be assigned.

So it's good practice to assign an initial value to the variable so that you can effectively ensure that typeof returns "undefined" only in one case: when the variable is not declared.

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.