JavaScript Null object detailed

Source: Internet
Author: User

Web page effects Null object detailed

Null

This is an object, but is empty. Because it is an object, typeof null returns ' object '.

Null is a JavaScript reserved keyword.

Null is automatically converted to 0 when it participates in numeric operations, so the following expression evaluates to the correct value:

Expression: 123 + null result value: 123

Expression: 123 * Null result value: 0

The following are not the correct methods:
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 zero, or false, the result will be the same as NULL, although NULL is not the same as the two. Note: This method can be used to determine both null, undefined, number 0, and false.
var exp = null;
if (typeof exp = "NULL")
{
Alert ("is null");
}

For backward compatibility, when EXP is null, typeof null always returns object, so this cannot be judged.
var exp = null;
if (IsNull (exp))
{
Alert ("is null");
}

There is IsNull this function in VBScript, but not in JavaScript.

The following are the correct methods:
var exp = null;
if (!exp && typeof exp!= "undefined" && exp!= 0)
{
Alert ("is null");
}

typeof exp!= "undefined" excludes undefined;
Exp!= 0 excludes the number zero and false.

The simpler and the right way:
var exp = null;
if (exp = = NULL)
{
Alert ("is null");
}

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.