Home >Web Front-end >JS Tutorial >Detailed analysis of the difference between js judging undefined type, undefined, null,_javascript skills

Detailed analysis of the difference between js judging undefined type, undefined, null,_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:09:051305browse

js determines undefined type

Today, use showModalDialog to open the page and return the value. When the open page clicks the close button or directly clicks the close button on the browser, the return value is undefined
, so make your own judgment

var reValue=window.showModalDialog("","","");
if (reValue== undefined){
alert("undefined");
}

I found that I couldn’t tell, so I finally checked the information and needed to use typeof

Method:
if (typeof(reValue) == "undefined") {
alert("undefined");
}
typeof returns a string , there are six possibilities: "number", "string", "boolean", "object", "function", "undefined"


The difference between undefined, null and NaN in js

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

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

2. Comparison operation
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, nor is it equal to itself.

JavaScript undefined property

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

Syntax
undefined

Explanation
You cannot use a for/in loop to enumerate undefined properties, nor can you use the delete operator 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
Tip: You can only use the === operator to test whether a value is undefined, because the == operator considers undefined values ​​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

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