Summary of differences between null, NaN, and undefined in Javascript

Source: Internet
Author: User
Tags type null

1. Type Analysis:
The data types in js include undefined, boolean, number, string, and object. The first four are original data types and the second are reference data types.

Copy codeThe Code is 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); // display "undefined"
Alert (typeof a1); // display "undefined"
Alert (typeof a2); // displays "boolean"
Alert (typeof a3); // display "number"
Alert (typeof a4); // display "string"
Alert (typeof a5); // displays "object"
Alert (typeof a6); // displays "object"
Alert (typeof a7); // display "number"
Alert (typeof a8); // display "undefined"

From the code above, we can see that undefined is undefined for undefined values and definitions. null is a special object and NaN is a special number.
2. Comparison Copy codeThe Code is as follows: var a1; // The a1 value 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 code above, we can draw a conclusion: (1) undefined is equal to null; (2) NaN is not equal to any value and is not equal to itself.

JavaScript undefined attributes

Definition and usage
The undefined attribute is used to store the undefined value of JavaScript.


You cannot use the for/in loop to enumerate the undefined attribute, or use the delete operator to delete it.
Undefined is not a constant. You can set it to another value.
Undefined is returned when you try to read non-existent object attributes.
Tips and comments
<The value is equivalent to the undefined operator that the operation is used to test whether a value is undefined, because = "=">
<Indicates no value, but>

In this example, we will check the undefined one of the two variables:Copy codeThe Code is 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:

[Supplement] Null data type
In Jscript, the Data Type null has only one value: null. The null keyword cannot be used as the name of a function or variable.
A variable that contains null contains "no value" or "no object ". In other words, the variable does not save valid numbers, strings, boolean, arrays, or objects. You can assign a null value to a variable to clear the content of the variable.

Note that in Jscript, null and 0 are not equal (different from C and C ++ ). It should also be noted that the typeof operator in Jscript reports the null value as the Object type, rather than the type null. This potential obfuscation is intended for backward compatibility.
Undefined Data Type
The undefined value is returned as follows:
The object property does not exist,
Declared variables but never assigned values.

Note that you cannot compare it with undefined to test whether a variable exists, although you can check whether its type is "undefined ". In the following code example, assume that the programmer wants to test whether the variable x has been declared:Copy codeThe Code is as follows: // This method does not work
If (x = undefined)
// Perform some operations
// This method also does not work-check required
// String "undefined"
If (typeof (x) = undefined)
// Perform some operations
// This method is valid
If (typeof (x) = "undefined ")
// Perform some operations

Consider comparing the undefined value with null.
SomeObject. prop = null;
In the following cases, the comparison result is true,
If the property 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 the property 'prop'
In JavaScript, null and undefined were once confusing. The following analysis helps you better understand it (or confuse you ):
-Null is the keyword; undefined is an attribute of the Global object.
-Null is an object (empty object without any attributes or methods); undefined is a value of the undefined type. Try the following code:
Document. writeln (typeof null); // return object
Document. writeln (typeof undefined); // return undefined
-In the Object model, all objects are instances of objects or their subclasses, except for null objects:
Document. writeln (null instanceof Object); // return false
-Null "equivalent (=)" indicates undefined, but not "full equivalent (=)" indicates undefined:
Document. writeln (null = undefined); // return true
Document. writeln (null === undefined); // return false
-During the operation, both null and undefined can be converted to false, but not equal 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.