The information collected is as follows:
1. Judge undefined:
Copy CodeThe code is as follows:
<span style= "Font-size:small;" >var tmp = undefined;
if (typeof (tmp) = = "undefined") {
Alert ("undefined");
}</span>
Description: typeof returned a string of six possible types: "Number", "string", "Boolean", "Object", "function", "undefined"
2. Determine null:
Copy CodeThe code is as follows:
<span style= "Font-size:small;" >var tmp = NULL;
if (!tmp && typeof (TMP)! = "undefined" && tmp!=0) {
Alert ("null");
} </span>
3. Judge Nan:
Copy CodeThe code is as follows:
<span style= "Font-size:small;" >var tmp = 0/0;
if (IsNaN (TMP)) {
Alert ("NaN");
}</span>
Note: If you compare Nan to any value (including itself), the result is false, so to determine whether a value is Nan, you cannot use the = = or = = = operator.
Tip: the IsNaN () function is often used to detect the results of parsefloat () and parseint () to determine whether they represent a valid number. Of course, you can also use the IsNaN () function to detect arithmetic errors, such as using 0 to divide the case.
4. Judge undefined and null:
Copy CodeThe code is as follows:
<span style= "Font-size:small;" >var tmp = undefined;
if (tmp== undefined)
{
Alert ("null or undefined");
} </span>
Copy CodeThe code is as follows:
<span style= "Font-size:small;" >var tmp = undefined;
if (tmp== null)
{
Alert ("null or undefined");
}</span>
Description: null==undefined
<!--endfragment-->
5. Judge undefined, null, and Nan:
Copy CodeThe code is as follows:
<span style= "Font-size:small;" >var tmp = NULL;
if (!tmp)
{
Alert ("null or undefined or NaN");
}</span>
js method for judging nan null undefined