JS learning path: how to judge undefined and null in JS

Source: Internet
Author: User
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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.