Summary of differences between undefined, null, "", 0 value, and false in JavaScript

Source: Internet
Author: User
Tags type null

In the various data types defined in the programming language, we will define a null value or a false value for it, such as a null value for the object type null,.net the null value of the database field in the framework Dbnull,boolean false value of the type, and so on. There are also many kinds of "null" and "false values" in JavaScript, so what do they have in common and different points?

In fact, in the title I have listed all the "null" and "False value" in JavaScript, in addition to the Boolean value itself is true and false in the two cases, the other data types of "null" is mainly undefined and defined the two major categories. The types of these null values are:

The common denominator of these five values is that, in the IF statement, the false branch will be executed. Of course, in a broad sense, it means that these values are invalid or null values on their corresponding data types. And these five values! operation, the result is all: true.

There are also differences in these values, where undefined and null are special, although the type of NULL is object, but Null does not have any attributes of the object, that is, we are not able to perform null.tostring (), The default invocation of an object instance such as Null.constructor. So in this sense, null and undefined have the greatest similarity. Look at the null = = undefined result (true) to be more descriptive. But similar to similar, there is a difference, that is, and the number operation, the result of the + NULL is: 10;10 + undefined result is: NaN.

In addition, "", 0, and false although the IF statement is represented as "false value", they are all meaningful data, but only as "null" or "False value" because: "". ToString (), (0). ToString () and false.tostring () are valid executable expressions.

In fact, these 5 values in the above mentioned in these differences, not too will give process control too big problem, so what to distinguish them? It is important to note that the differences between these values are larger when converted to string, and their conversion relationships to string are:

This conversion relationship requires special attention when doing string accumulation, no, this will cause some unexpected problems, and today we are going to get confused by the default to NULL.

Summary of differences between undefined, null, "", 0 value, and false in JavaScript

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.