Distinguish undefined, null, and Nan in Javascript

Source: Internet
Author: User

Distinguish underfined, null, and Nan

First, let's look at the running result of the following statement:

 
1: VaRA;
 
2:Alert ();// Display "undefined"
 
3:Alert (TypeofA );// Display "undefined"
 
4:Alert (B); // display"Undefined"

The value and type of a are both 'underfined'

 
1: VaRA =Null;
 
2:Alert ();// Display "null"
3:Alert (TypeofA); // display"Object"

The preceding description is a null variable with a null value and an object type.

 
1: VaRB;
 
2:Alert (Null= B );// Display "true"
 
3:Alert (Null= Undefined); // display"True"

The preceding description shows that undefined and null are equal.

 
1: VaRA1;// The values and types are both 'underfined'
2: VaRA2 =Null;// The value is null and the type is object.
 
3:Alert (a1 = a2); // display"True"

The preceding two examples show that null and underfind are very similar. At least undefined and null are equal.

 
1:Alert (100 +Null);// Display 100"
 
2:Alert (100 + undefined); // display"Nan" 

The preceding description shows that null and underfind are similar, but there are still residential areas!

Underfined: Unknown variable name or unassigned variable.
Null: Special object
Nan: Special Number

1:Alert (Typeof(Undefined ));// Display 'undefined'
 
2:Alert (Typeof(Null));// Display 'object'
 
3:Alert (Typeof(""));// Display 'string'
 
4:Alert (Typeof(0 ));// Display 'number'
 
5:Alert (Typeof(False));// Display 'boolean'
6: 
 
7: VaRA7 = Nan;
 
8: VaRA8 = undefined;
 
9: 
 
10:Alert (TypeofA7 );// Display "Number"
 
11:Alert (TypeofA8); // display"Undefined"
The preceding description shows that Nan is a special number, which is not equal to null or undefined.

Function parameter judgment

Let's take a look at the following example:

 
1:Test =Function()
2:{
 
3:Alert (TypeofA );
 
4:}
 
5:
 
6:Test ();// If no parameter is set, the running result is "underfined"
 
7:Test (Null); // An empty parameter is passed and the running result is"Object"If you want to use a. Length and so on, an error is reported because the object is empty!

The correct parameter check is:

 
1:Test =Function(V)
 
2:{
3: If(V! =Null&&TypeofV! ='Undefined')
 
4:{
 
5: // Use the attributes of V, such as V. Length and V. Property.
 
6: // If (V. offsetx! = Undefined)
 
7: // If (value = undefined)
 
8:}
 
9:}

Of course, you can also use if (v) to simplify the encoding. You can check whether V is not set to or if V is null. However, note that if a Boolean of true or false is input, if (v) means if (V = true ),CodeThe logic may not be the result you want!

    • Extjs + ASP. NET implements a real progress bar to display the progress of the server's long-term operations
    • Extjs + ASP. NET Implement Asynchronous Tree node search and find next (findnext)
    • Extjs + ASP. NET implement tree node dragging (dragdrop)
    • Extjs + ASP. NET implement the drag from grid to tree (dragdrop)
    • Use Yui compressor and DOS batch processing scripts to compress JavaScript and CSS
    • Optimize Javascript
    • Distinguish underfined, 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.