JavaScript has two special data types: undefined and null. The following section describes the null judgment and the undefined judgment. The following is an incorrect usage: varexpundefined; if (expundefined) {alert JavaScript has two special data types: undefined and null. Next we will introduce the null judgment. Next we will discuss the undefined judgment.
Incorrect usage:
Var exp = undefined;
If (exp = undefined)
{
Alert ("undefined ");
}
If exp is null, the same result is obtained as undefined, although null and undefined are different. Note: This method can be used to determine both undefined and null.
Var exp = undefined;
If (typeof (exp) = undefined)
{
Alert ("undefined ");
}
Typeof returns a string, which may be "number", "string", "boolean", "object", "function", or "undefined"
The following are the correct usage:
Var exp = undefined;
If (typeof (exp) = "undefined ")
{
Alert ("undefined ");
}
--------------------------------------------------------------------------------
How to judge null in JS
Incorrect usage:
Var exp = null;
If (exp = null)
{
Alert ("is null ");
}
When exp is undefined, it will also get the same result as null, although null and undefined are different. Note: This method can be used to determine both null and undefined.
Var exp = null;
If (! Exp)
{
Alert ("is null ");
}
If exp is undefined or the number is zero, the result is the same as null, although null is different from the two. Note: You can use this method to determine whether null, undefined, and numeric values are zero at the same time.
Var exp = null;
If (typeof (exp) = "null ")
{
Alert ("is null ");
}
For backward compatibility, if exp is null, typeof always returns the object.
Var exp = null;
If (isNull (exp ))
{
Alert ("is null ");
}
No isNull function in JavaScript.
The following are the correct usage:
Var exp = null;
If (! Exp & typeof (exp )! = "Undefined" & exp! = 0)
{
Alert ("is null ");
}
However, in DOM applications, we generally only need to use (! Exp) to determine, because in the DOM application, null may be returned, undefined may be returned, if the specific judgment of null or undefined will make the program too complex.