Distinguish JS in the Undefined,null, "", 0 and False

Source: Internet
Author: User
Tags type null

Distinguish JS in the Undefined,null, "", 0 and False

Today, a colleague asked me a JS question, if (0 = = ") is true very do not understand, a separate test, recorded in this.

<script>Alert (0 = = ");//trueAlert (0 = =false);//trueAlertfalse= = ");//trueAlertNULL= = undefined);//trueAlert (!0);//trueAlert (!false);//truealert (!undefined);//trueAlert (!NULL);//trueAlert (! ');//trueAlert (0 = = undefined);//falseAlert (0 = =NULL);//falseAlertfalse==NULL);//falseAlertfalse= = undefined);//falseAlert (' = =NULL);//falseAlert (' = = = undefined);//false</script>

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, 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:true。

There are also differences in these values, where undefined andNULLMore special, thoughNULLis of type object, butNULLDoes not have an attribute of any object, which means that we cannot executeNULL. toString (),NULLDefault invocation of object instances such as. Constructor. So in this sense,NULLAnd undefined have the greatest similarity. LookNULL= = Results of undefined (true) is more illustrative of this. But similar to similar, there is a difference, that is, and the number of operations, 10 +NULLThe result is: 10;10 + undefined result: NaN.

In addition "", 0 andfalseAlthough the IF statements behave as "false values", they are all meaningful data and are only used as "null" or "false values" because: "". ToString (), (0). ToString () andfalse. ToString () is a valid executable expression.

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:

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


This conversion relationship needs special attention when doing string accumulation, no, this will cause some unexpected problems, and today it is a bit confusing to get a null by default to "null".

Distinguish JS in the Undefined,null, "", 0 and False

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.