The difference between Undefined,null,nan 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.
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); Show "string"
Alert (typeof A5); Show "Object"
Alert (typeof A6); Show "Object"
Alert (typeof A7); Show "Number"
Alert (typeof A8); 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 Operation
Var A1;//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 "t Rue "
Alert (a2 = = A3);//display" false "
alert (a2! = a3);//Display" true "
Alert (a3 = A3);//display" false "
Alert (a3! = A3); Display "true"
from the above code can conclude that: (1) undefined is equal to NULL, (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
Tip: You can use the = = = operation to test whether a value is undefined because the = = operator considers the undefined value equivalent to null.
Note: null means no value, and undefined represents an undeclared variable, or a variable that has been declared but not assigned a value, or an object property that does not exist.
Instance
In this example, we will detect one of the two variables that are not defined:
<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:
T2 is undefined

The difference between Undefined,null,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.