Undefined,null in JavaScript, "", 0 and false for the cloud

Source: Internet
Author: User

In a variety of data types, we will define a "null" or "false value" for it, such as a null value for the object type null, a null value for the database field in the. NET Framework DBNull, a Boolean type of false value False 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, except for the Boolean value itself is true and false , the other data type "NULL" Mainly undefined and defined are the two major categories. The types of these null values are:

typeof (undefined) = = ' undefined '
typeof (null) = = ' object '
typeof ("") = = = ' String '
typeof (0) = = ' Number '
typeof (false) = = ' Boolean '


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, which means that we cannot execute Null. toString () The default invocation of an object instance. 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 () is a valid executable expression.

in fact, these 5 values in the above-mentioned differences, is not very much to the process control caused too much 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:

String (undefined), "undefined"
String (NULL), "NULL"
String ("") , ""
String (0), "0"
String (False), "false"


This conversion relationship requires special attention when doing string accumulation, otherwise there will be some unexpected problems.

Undefined,null in JavaScript, "", 0 and false in the cloud

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.