Distinguish the null value of JS

Source: Internet
Author: User
Tags type null

Distinguish JS in the Undefined,null, "", 0 and False
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?
The undefined type has only one value, which is undefined. When the declared variable has not been initialized, the default value for the variable is undefined.
The null type also has only one value, which is null. Null is used to represent an object that does not already exist, and is commonly used to indicate that a function is attempting to return a nonexistent object.

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:
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, 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 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"

Js Sample Code

1. var ovalue;
2. Alert (Ovalue = = undefined); Output "true"

This code shows true and the value representing Ovlaue is undefined because we did not initialize it.
JS Code

3. alert (Null = = document.getElementById (' notexistelement '));

When a DOM node with ID "notexistelement" does not exist on the page, this code shows "true" because we are trying to get a nonexistent object.
JS Code

4. Alert (typeof undefined); Output "undefined"
5. Alert (typeof null); Output "Object"

The first line of code is easy to understand, the type of undefined is undefined, and the second line of code is confusing, why is the type of NULL another object? In fact, it was a mistake originally implemented by JavaScript, which was later ECMAScript. Today we can explain that NULL is a placeholder for an object that does not exist, but it is important to be aware of this feature when actually coding.
JS Code

6. alert (null = = undefined); Output "true"

ECMAScript that undefined are derived from null, so they are defined as equal. But what if, in some cases, we must differentiate between these two values? You can use the following two methods.
JS Code

7. alert (Null = = = undefined); Output "false"
8. alert (typeof null = = typeof undefined); Output "false"

Using the TypeOf method, as mentioned earlier, NULL is not the same as the type of undefined, so the output is "false". and = = = Absolute equals, where null = = = undefined output false.

Distinguish the null value of JS

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.