This article mainly introduces how to judge null, undefined, and NaN in JS. If you need it, you can refer to the following str = "s" ++;
Then Nan appears. Find it for a while.
The collected data is judged as follows:
1. Determine undefined:
The Code is as follows:
Var tmp = undefined;
If (typeof (tmp) = "undefined "){
Alert ("undefined ");
}
Note: typeof returns a string. There are six possible types: "number", "string", "boolean", "object", "function", and "undefined"
2. null judgment:
The Code is as follows:
Var tmp = null;
If (! Tmp & typeof (tmp )! = "Undefined" & tmp! = 0 ){
Alert ("null ");
}
3. Judge NaN:
The Code is as follows:
Var tmp = 0/0;
If (isNaN (tmp )){
Alert ("NaN ");
}
Note: If the result of comparing NaN with any value (including its own) is false, you cannot use the = OR = Operator to determine whether a value is NaN.
Tip: The isNaN () function is usually used to check the results of parseFloat () and parseInt () to determine whether they represent valid numbers. Of course, you can also use the isNaN () function to detect arithmetic errors, such as using 0 as the divisor.
4. Determine undefined and null:
The Code is as follows:
Var tmp = undefined;
If (tmp = undefined)
{
Alert ("null or undefined ");
}
The Code is as follows:
Var tmp = undefined;
If (tmp = null)
{
Alert ("null or undefined ");
}
Description: null = undefined
5. Determine undefined, null, and NaN:
The Code is as follows:
Var tmp = null;
If (! Tmp)
{
Alert ("null or undefined or NaN ");
}
Tip: Generally, this is enough to differentiate data.