There are two special data types in JavaScript: undefined and null, and let's talk about undefined's judgment.
JS Judge undefined type
The code is as follows |
Copy Code |
if (revalue== undefined) { Alert ("undefined"); } |
Found that the judge does not come out, finally checked the data to use typeof
Method:
The code is as follows |
Copy Code |
if (typeof (revalue) = = "undefined") { Alert ("undefined"); }
|
typeof returns a string of six possible: "Number", "string", "Boolean", "Object", "function", "undefined"
The following is an incorrect usage:
The code is as follows |
Copy Code |
var exp = undefined; if (exp = = undefined) { Alert ("undefined"); } |
When exp is null, it also gets the same result as undefined, although null and undefined are different. Note: This method can be used to determine both undefined and null.
The following are the correct uses:
The code is as follows |
Copy Code |
var exp = undefined; if (typeof (exp) = = "undefined") { Alert ("undefined"); } |
Pay attention to whether it is undefined to be sure to add quotation marks on both sides, otherwise it will not succeed (personal test results!)
instance
A variable var bank_value, when alert him, some of his values are undefined, so I use the following to determine whether he equals undefined.
The code is as follows |
Copy Code |
var Yinvalue; if (yinvalue== ' undefined ') { Break } |
It turned out to be wrong, without entry conditions. Later on the Internet check, very simple as follows:
The code is as follows |
Copy Code |
if (typeof (yinvalue) = = ' undefined ') { Break } |
typeof returns a string of six possible: "Number", "string", "Boolean", "Object", "function", "undefined"