Distinguish JS in the Undefined,null, "", 0 and False_javascript tips

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

In fact, I've listed all the "null" and "false" values in JavaScript, except that the Boolean value itself is true and false, "null values" for other data types are mainly undefined and defined. The types of these null values are:
Copy Code code as follows:

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

What these five values have in common is that the false branch is executed in the IF statement. In broad terms, of course, these values are either invalid values or null values on their corresponding data types. And these five values are done! operation, the result is all: true.

These values are also different, where undefined and null are special, although the type of NULL is object, but Null does not have any attributes of any object, which means that we are not able to perform null.tostring (), The default invocation of object instances such as Null.constructor. So in this sense, null and undefined have the greatest similarity. The result of the NULL = = undefined (true) is much more descriptive. But similarity is similar, there is a difference, that is, and the number of operations, the result of a + null: 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 and are only used as "null value" or "False value" because: "". ToString (), (0). ToString () and false.tostring () are valid executable expressions.

In fact, these 5 values in the above mentioned differences, not too much to the process control caused by the problem, so what to distinguish them? Note that the difference between these values when converting to string is relatively large, and their conversion to string is:
Copy Code code as follows:

String (undefined)-> "undefined"
String (NULL)-> "NULL"
String ("")-> ""
String (0)-> "0"
String (False)-> "false"

This conversion relationship requires special attention when doing string addition, and this can be a bit of an unexpected problem, and today it's been confusing for a while that a NULL has been converted to NULL by default.

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.