Js determines the difference between undefined type undefined and null

  • 2020-03-30 00:55:44
  • OfStack

Js determines the undefined type

When the page opens today with the showModalDialog and returns a value. The return value is undefined when a page opens by clicking the close button or clicking close on the browser
So wise wise & cake;  

Var reValue = window. ShowModalDialog (" ", ""," ");
  If (reValue = = undefined) {
  Alert (" undefined ");

It was found that the judgment could not be made, so typeof was used to check the data at last

If (typeof(reValue) == "undefined") {
      Alert (" undefined ");
Typeof returns a string, with six possibilities: "number", "string", "Boolean", "object", "function", "undefined"

Undefined,null,NaN in js

1. Type analysis:
Js have undefined the data types, number, Boolean, string, object and so on five, four for the primitive type, before 5 kinds of reference types.
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 the "number"
Alert (typeof a4); / / show "string"
Alert (typeof a5); / / show "object"
Alert (typeof a6); / / show "object"
Alert (typeof a7); / / show the "number"
Alert (typeof a8); / / show "undefined"

From the above code, you can see that undefined and undefined values are undefined, null is a special object, and NaN is a special number.

2. Comparison operations
Var a1.                 //a1 is undefined
Var a2 = null;
Var a3 = NaN;
Alert (a1 = = a2); / / show "true"
Alert (a1! = a2); / / show "false"
Alert (a1 = = a3); / / show "false"
Alert (a1! = a3); / / show "true"
Alert (a2 = = a3); / / show "false"
Alert (a2! = a3); / / show "true"
Alert (a3 = = a3); / / show "false"
Alert (a3! = a3); / / show "true"

From the above code we 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 attribute

Definition and usage
The undefined property is used to hold the undefined value of JavaScript.


The for/in loop cannot be used to enumerate the undefined property, nor can the delete operator be used to delete it.
Undefined is not a constant, you can set it to something else.
Undefined is also returned when an attempt is made to read a property of a nonexistent object.

Hints and comments
Tip: you can only use the === operation to test whether a value is undefined, because the == operator considers an undefined value to be null.
Note: null means no value, and undefined means an undeclared variable, or a declared but unassigned variable, or a nonexistent object property.

The instance
In this example, we will detect the undefined one of the two variables:
< The 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>

T2 is undefined

Related articles: