Home  >  Article  >  Web Front-end  >  Detailed explanation of usage examples of the difference between undefined, null, NaN, etc. in JavaScript

Detailed explanation of usage examples of the difference between undefined, null, NaN, etc. in JavaScript

伊谢尔伦
伊谢尔伦Original
2017-07-18 13:43:361227browse

The difference between undefined, null and NaN in js

1. Type analysis:
The data types in js include undefined, boolean, number, There are 5 types such as string and object. The first 4 types are primitive types, and the fifth type is 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);    //显示"undefined"
alert(typeof a1); //显示"undefined"
alert(typeof a2); //显示"boolean"
alert(typeof a3); //显示"number"
alert(typeof a4); //显示"string"
alert(typeof a5); //显示"object"
alert(typeof a6); //显示"object"
alert(typeof a7); //显示"number"
alert(typeof a8); //显示"undefined"

It can be seen from the above code that undefined values ​​and unassigned values ​​are undefined, null is a special object, and NaN is a special number.

2. Comparison operation

var a1;         //a1的值为undefined
var a2 = null;
var a3 = NaN;
alert(a1 == a2); //显示"true"
alert(a1 != a2); //显示"false"
alert(a1 == a3); //显示"false"
alert(a1 != a3); //显示"true"
alert(a2 == a3); //显示"false"
alert(a2 != a3); //显示"true"
alert(a3 == a3); //显示"false"
alert(a3 != a3); //显示"true"

From the above code we can draw the conclusion: (1) undefined and null are equal; (2) NaN and any The values ​​are not equal, nor are they equal to themselves.

JavaScript undefined attribute

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

Syntax
undefined

Explanation
You cannot use for/in loops to enumerate undefined properties, nor can you use the delete operation character to delete it.
undefined is not a constant and can be set to other values.
Undefined will also be returned when trying to read non-existent object properties.

Tips and Notes
Tip: You can only use the === operator to test whether a value is undefined, because the == operator considers the undefined value to be equivalent to null .
Note: null means no value, and undefined means an undeclared variable, or a variable that has been declared but has no value assigned, or an object property that does not exist.

Example
In this example, we will detect that one of the two variables is undefined:

Output:
t2 is undefined

Judgment undefined:

var tmp = undefined; 
if (typeof(tmp) == "undefined"){ 
alert("undefined"); 
}

Judgment null:

var tmp = null; 
if (!tmp && typeof(tmp)!="undefined" && tmp!=0){ 
alert("null"); 
} 

Judgment NaN:

var tmp = 0/0; 
if(isNaN(tmp)){ 
alert("NaN"); 
}

Description: If NaN is combined with any value (including itself) The results obtained by comparison are all false, so to determine whether a value is NaN, you cannot use the == or === operator.

Tip: The isNaN() function is usually used to detect the results of parseFloat() and parseInt() to determine whether they represent legal numbers. Of course, you can also use the isNaN() function to detect arithmetic errors, such as using 0 as a divisor.

The above is the detailed content of Detailed explanation of usage examples of the difference between undefined, null, NaN, etc. in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn