The difference between null, Nan, and undefined in Javascript

Source: Internet
Author: User
Tags type null

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

Code highlighting produced by Actipro Codehighlighter (freeware) http://Www.CodeHighlighter.com/-->var A1;
varA2 =true;
vara3 = 1;
vara4 = "Hello";
varA5 =NewObject ();
varA6 =NULL;
vara7 = NaN;
vara8 = undefined;

Alerttypeofa);//Show "undefined"
AlerttypeofA1);//Show "undefined"
AlerttypeofA2);//Show "Boolean"
AlerttypeofA3);//Show "Number"
AlerttypeofA4);//Show "string"
AlerttypeofA5);//Show "Object"
AlerttypeofA6);//Show "Object"
AlerttypeofA7);//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 highlighting produced by Actipro Codehighlighter (freeware) http://Www.CodeHighlighter.com/-->var A1; 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:

Code highlighting produced by Actipro Codehighlighter (freeware) http://www.CodeHighlighter.com/--><script type= "Text/javascript" >
varT1= ""
varT2

if(t1===undefined) {document.write ("T1 is undefined")}
if(t2===undefined) {document.write ("T2 is undefined")}

</script> output:

T2 isundefined

Added

Null data type
In Jscript, the data type is NULL with only one value: null. The keyword NULL cannot be used as the name of a function or variable.

A 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 in Jscript, NULL is not equal to 0 (unlike in C and C + +). It should also be noted that the typeof operator in JScript will report a null value of type Object, not type null. This potential confusion is for backwards compatibility.

Undefined Data types
The undefined value is returned as follows:

object property does not exist,
A variable was declared but never assigned a value.
Note that 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 if the variable x has been declared:

This method does not work
if (x = = undefined)
Do something
This method also does not work-must be checked
string "Undefined"
if (typeof (x) = = undefined)
Do something
This method works
if (typeof (x) = = "undefined")
Do something
Consider comparing the undefined value to null.

Someobject.prop = = NULL;
The result of the comparison is true when the following occurs,

If the property someobject.prop contains a null value,
If the attribute Someobject.prop does not exist.
To check whether an object property exists, you can use the new in operator:
if ("prop" in Someobject)
Someobject has attribute ' prop '

In JavaScript, null and undefined were once confusing. The following analysis helps you to recognize it more clearly (or to make you more confused):

-NULL is a keyword; undefined is a property of the global object

-NULL is an object (Null 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); 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:
Document.writeln (null = = undefined); return True
Document.writeln (Null = = = undefined); return False

-When the operation is 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.