The difference between null, Nan, and undefined in Javascript

Source: Internet
Author: User

1. Type analysis:

JS data types have undefined,boolean,number,string,object, such as 5, the first 4 are the original type, the 5th is the reference type.

Code:

varA1;
varA2= true;
varA3= 1;
varA4= "Hello";
varA5= NewObject ();
varA6= NULL;
varA7=NaN;
varA8=undefined;

Alert (typeofa); //Show "undefined"
Alert (typeofA1); //Show "undefined"
Alert (typeofA2); //Show "Boolean"
Alert (typeofA3); //Show "Number"
Alert (typeofA4); //Show "string"
Alert (typeofa5); //Show "Object"
Alert (typeofa6); //Show "Object"
Alert (typeofA7); //Show "Number"
Alert (typeofA8); //Show "undefined"


You can see from the above code that undefined values and definitions are unassigned for Undefined,null is a special Object,nan is a special number.

2. Comparison operations

Code:

varA1; //the value of A1 is undefined
varA2= NULL;
varA3=NaN;

Alert (A1==A2); //Show "true"
Alert (A1!=A2); //Show "false"

Alert (A1==A3); //Show "false"
Alert (A1!=A3); //Show "true"

Alert (A2==A3); //Show "false"
alert (A2!=A3); //Show "true"

Alert (A3==A3); //Show "false"
Alert (A3!=A3); //Show "true"


From the above code, it can be concluded that (1) undefined is equal to NULL, and (2) Nan is not equal to any value, nor is it equal to itself.

JavaScript Undefined Properties

Definition and usage

The undefined property is used to store JavaScript undefined values.

Grammar
Undefined
Description

You cannot use the For/in loop to enumerate the undefined property, nor can you delete it by using the delete operator.

Undefined is not a constant, it can be set to another value.

Undefined is also returned when attempting to read an object property that does not exist.

Hints and Notes

The < value is equivalent to the undefined operator, which considers the operation to test whether a value is undefined, because = "=" = "= =" >

< means no value, and >

Instance

In this example, we will detect one of the two variables that are not defined:

<script type= "Text/javascript" >var t1= "" var t2if (t1===undefined) {document.write ("T1 is undefined")}if (t2=== Undefined) {document.write ("T2 is undefined")}</script> output:
T2 is undefined

Added
null  data type   data type  null  only one value in  Jscript : null. Keyword  null  cannot be used as a name for a function or variable.   The variable containing  null  contains either "No value" or "no object". In other words, the variable does not hold a valid number, String, Boolean, array, or object. You can clear the contents of a variable by assigning a  null  value to a variable.   Note that,null  and  0  are not equal in  Jscript  (unlike in  C  and  C++ ). It should also be noted that the  typeof  operator in JScript will report  null  values as  Object  types, not type  null. This potential confusion is for backwards compatibility.  Undefined  data type   The return  undefined  Value:  object property does not exist,  declares a variable but never assigns a value.   Note You cannot test for the existence of a variable by comparing it with  undefined , although you can check whether its type is "undefined". In the following code example, suppose the programmer wants to test whether a variable has been declared  x : //  this method does not work  if  (x == undefined)       //  do some operations  //  This method also does not work-  must check  //  string   "undefined"  if  (typeof (X)  == undefined)      //  for some operations  //  This method is effective  if  (typeof (x)  ==  "undefined")      // Do some things   consider comparing  undefined  values to null.  someObject.prop == null;  the results of the comparison are  true,  if the properties  someObject.prop  include  null  Value,  If property  someObject.prop  does not exist.   to check if an object property exists, you can use the new  in  operator: if  ("prop"  in someobject)  //  someobject  has attributes   ' prop '   in JavaScript, null and undefined were once confusing. The following analysis helps you to understand it more clearly (or to make you more confused):  - null is the keyword; undefined is a property of the global object  - null is an object (empty object,  No properties and methods); undefined is a value of type undefined. Try the following code:         document.writeln (typeof null);  //return  object        document.writeln (typeof undefined);  // In the return undefined -  object model, all objects are instances of object or its subclasses, but null object exceptions:         document.writeln (null instanceof object);  //return false - null "equivalent (= =) "In Undefined, but not" full equivalent (= = =) "In UNDEFINED:&NBSP;&NBsp;      document.writeln (null == undefined);  //return true         document.writeln (null === undefined);  //return  false -  null and undefined can be converted to false by type, but not equivalent to false:         document.writeln (!null, !undefined); //return true,true         document.writeln (Null==false); //return false         document.writeln (undefined==false);  //return false


The difference between null, Nan, and undefined 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.