The difference between null, Nan, and undefined in Javascript summary _javascript tips

Source: Internet
Author: User
Tags type null

1. Type analysis :
The data type in JS has 5 kinds of undefined,boolean,number,string,object, the first 4 is the original type, and the 5th type is reference.

code as follows:

var A1;
var a2 = true;
var a3 = 1;
var a4 = "Hello";
var A5 = new Object ();
var a6 = null;
var a7 = NaN;
var a8 = undefined;
Alert (typeof a); Show "Undefined"
Alert (typeof A1); Show "Undefined"
Alert (typeof A2); Show "Boolean"
Alert (typeof A3); Show "Number"
Alert (typeof A4); Display "string"
Alert (typeof A5); Show "Object"
Alert (typeof A6); Show "Object"
Alert (typeof A7); Show "Number"
Alert (typeof A8); Show "Undefined"

As you can see from the code above, undefined values and undefined undefined,null is a special Object,nan is a special number.
2. Comparison Operation
Copy Code code as follows:

var A1; The value of A1 is undefined
var a2 = null;
var a3 = NaN;
alert (a1 = = A2); Display "true"
Alert (A1!= A2); Display "false"
alert (a1 = = A3); Display "false"
Alert (A1!= A3); Display "true"
alert (A2 = = A3); Display "false"
Alert (A2!= A3); Display "true"
Alert (a3 = = A3); Display "false"
Alert (A3!= A3); Display "true"

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

JavaScript undefined Properties

Definitions and Usage
The undefined property is used to store the undefined value of JavaScript.


The undefined property cannot be enumerated using the for/in loop, nor can it be deleted with the delete operator.
Undefined is not a constant, you can set it to a different value.
Undefined is also returned when an attempt is to read an object property that does not exist.
Tips and comments
< value is equivalent to the undefined operator that the operation is considered to test whether a value is undefined because = "=" = "= =" >
< means no value, while >

In this case, we will detect one of the two variables not defined:
 code as follows:

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

Added Null data type
Data type null has only one value in Jscript: null. 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 it.

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 reports that the null value is of type Object, not type null. This potential confusion is for backward compatibility.
Undefined data type
Return the undefined value as follows:
object property does not exist,
A variable was declared but never assigned a value.

Note You cannot test whether a variable exists by comparing it to undefined, although you can check whether its type is "undefined". In the following code example, suppose the programmer wants to test whether the variable x has been declared:
 code as follows:

This method doesn't work.
if (x = = undefined)
Make some action
This method also does not work-must check
string "Undefined"
if (typeof (x) = = undefined)
Make some action
This method works.
if (typeof (x) = = "undefined")
Make some action

Consider comparing the undefined value with null.
Someobject.prop = null;
The result of the comparison is true when the following conditions
If the attribute Someobject.prop contains a null value,
If the property 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 attributes ' prop '
In JavaScript, null and undefined were once confusing. The following analysis will help you to perceive it more clearly (or to confuse you more):
-NULL is a keyword; undefined is a property of the global object
-NULL is an object (empty object with no properties and methods); Undefined is a undefined type value. Try the following code:
Document.writeln (typeof null); Return object
Document.writeln (typeof undefined); return undefined
-All objects in the object model are instances of object or its subclasses, with the exception of NULL objects:
Document.writeln (null instanceof Object); return False
-NULL "equivalent (= =)" In Undefined, but not "full equivalence (= = =)" In undefined:
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

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: 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.