Differentiate undefined, null, & quot;, 0, and false in JS.

Source: Internet
Author: User

In various data types defined in the programming language, we define a "null" or "false value" for them, such as null value of the object type ,.. NET Framework. There are also many "null" and "false" types in JavaScript, so what are their similarities and differences?

In fact, I have already listed all the "null" and "false" values in JavaScript in the title, except that the values of boolean are true and false, the "null" values of other data types mainly include undefined and defined. The types of these null values are:
Copy codeThe Code is as follows:
Typeof (undefined) = 'undefined'
Typeof (null) = 'object'
Typeof ("") = 'string'
Typeof (0) = 'number'
Typeof (false) = 'boolean'

The common feature of these five values is that when you make judgments in the if statement, the false branch is executed. In a broad sense, it means that these values are invalid values or null values corresponding to their data types. There are also five values! Returns true.

These values are also different. Among them, undefined and null are special. Although the null type is object, null does not have any object features, that is, we cannot execute null. toString (), null. default call of object instances such as constructor. So in this sense, null and undefined have the greatest similarity. Look at the result of null = undefined (true) to better illustrate this. However, there is a difference between similarity and similarity, that is, the 10 + null result is 10, and the 10 + undefined result is NaN.

In addition, "", 0, and false although the if statement is expressed as "false value", they are all meaningful data, but are treated as "null" or "false value", because :"". toString (), (0 ). toString () and false. toString () is a valid executable expression.

In fact, the five values mentioned above do not bring too much problems to process control. So what should they be differentiated? Note that the differences between these values and String values are quite large. The conversion relationships between these values and String values are as follows:
Copy codeThe Code is as follows:
String (undefined)-> "undefined"
String (null)-> "null"
String ("")-> ""
String (0)-> "0"
String (false)-> "false"

This conversion relationship requires special attention when accumulating strings. If not, unexpected problems may occur. Today, a null is converted to "null" by default, which is confusing for a while.

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.