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:
There are 5 data types in js, including undefined, boolean, number, 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); //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 operations
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 JavaScript's undefined value.
grammar
undefined
illustrate
You cannot use a for/in loop to enumerate an undefined property, 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 an object property that does not exist.
Tips and Notes
Tip: You can only use the === operator to test whether a value is undefined, because the == operator considers an undefined value to be equivalent to null.
Note: null means no value, while undefined means an undeclared variable, 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:
<script type="text/javascript">
var t1=""
vart2
if (t1===undefined) {document.write("t1 is undefined")}
if (t2===undefined) {document.write("t2 is undefined")}
</script>
Output:
t2 is undefined