Home > Web Front-end > JS Tutorial > Summary of the differences between null, NaN and undefined in Javascript_javascript skills

Summary of the differences between null, NaN and undefined in Javascript_javascript skills

WBOY
Release: 2016-05-16 17:37:46
Original
1112 people have browsed it

1. Type analysis :
The data types in js include undefined, boolean, number, string, object, etc. The first 4 types are primitive types, and the fifth type is reference type.
Code

Copy code The 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); //Display "boolean" "
alert(typeof a3); //Display "number"
alert(typeof a4); //Display "string"
alert(typeof a5); //Display "object"
alert (typeof a6); //Display "object"
alert(typeof a7); //Display "number"
alert(typeof a8); //Display "undefined"

From the above code, we can see that undefined values ​​and unassigned values ​​are undefined, null is a special object, and NaN is a special number.
2. Comparison operation
Copy code The code is 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 we can draw the conclusion: (1) undefined and null are equal; (2) NaN is not equal to any value , not equal to oneself.

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 loop to enumerate undefined properties, nor can you use 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 a non-existent object property.
Tips and Notes
< The value is equivalent to undefined The operator is used to test whether a value is undefined, because ="=" ="==">
< means None value, while>
Copy code
The code is as follows:

Output:
[Supplement] Null data type
In Jscript, the data type null has only one value: null. The keyword null cannot be used as the name of a function or variable.
A variable containing null contains "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 will report null values ​​as type Object, not type null. This potential confusion is for backward compatibility.
Undefined data type
Undefined value is returned in the following situations:
The object property does not exist,
The variable is declared but never assigned a value.

Note that 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, assume that the programmer wants to test whether the variable

// This method does not work
if (x == undefined)
// Do something
// This method also does not work - must check
// String "undefined"
if (typeof(x) == undefined)
// Do some operations
// This method is valid
if (typeof(x) == "undefined ")
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template