Distinguish between Undefined,null and Nan in JavaScript

Source: Internet
Author: User

differentiate between Underfined,null and Nan

Let's look at the result of the following statement:

1: var A;
2: //Show "undefined"
3: alert (typeof//Show "undefined" )
4: alert (b); Show "Undefined"

The values and types of a above are ' underfined '

1: var null;
2: //Show "null"
3: alert (typeof a); Show "Object"

The above description assigns a null variable, the value is null, the type is Object

1: var b;
2: alert (null//display "true"
3: alert (null = = undefined); Show "true"

The above description undefined and null are equal

1: var //value and type are ' underfined '
2: var NULL //value is NULL, type is Object
3: alert (a1 = = A2); Show "true"

The above two examples show that null and Underfind are very similar, at least undefined and null are equal.

1: NULL //Show "+"
2: alert (+ undefined); Show "NaN"

The above description of NULL and Underfind although very similar, but there are small differences, hey!

underfined: Unknown variable name, or non-assigned variable.
NULL: Special Object
NaN: Special number

1: alert (typeof(undefined)); //Show ' undefined '
2: alert (typeof(null)); //Show ' object '
3: alert (typeof(")"); //Display ' string '
4: alert (typeof(0)); //Show ' number '
5: alert (typeof(false)); //Show ' Boolean '
6:
7: var a7 = NaN;
8: var a8 = undefined;
9:
: alert (typeof//display "number"
One : alert (typeof A8); Show "Undefined"
The above indicates that Nan is a special number, and null and undefined are not equal.

The function parameter is not and is empty judgment

Let's take a look at the following example:

1: function (a)
2: {
3: alert (typeof a);
4: }
5:
6: //No parameters, the operation result is "underfined"
7: Test (null); Passed empty parameters, the result is "Object", if you want to use a.length, etc., will be an error, because the object is empty!

The correct parameter check is:

1: function (v)
2: {
3: if NULL typeof ' undefined ')
4: {
5: //Using properties of V, such as V.length, V.property
6:
7: //if (value = = undefined)
8: }
9: }

Of course, you can also use if (v) to simplify the encoding, you can check V is not given and V is null case, but note that if passed in a Boolean true or FALSE, with if (v) means if (v = = true), the code logic may not be the result you want!

Distinguish between Undefined,null and Nan in JavaScript

Related Article

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.