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.