Javascript null and undefined

Source: Internet
Author: User

Null:

Null is a javascript keyword, indicating "null". It is a javascript built-in object that does not contain any value.

Alert (typeof (null); // The result is an object.

When declaring a variable, it can be used as a placeholder to prevent an error (var a = NULL, indicating that the value of the variable A is "null ").

VaR A = NULL; alert (a); // The result is null.

Undefined:
Undefined is not a javascript keyword. It indicates that a variable has been declared but not initialized.

VaR A; alert (a); // The result is undefined.

Here I personally think that undefined itself is a value and its type is "undefined". In this case, there may be some logging ports, but let's look at the following example:

VaR A; alert (typeof (undefined); // The undefined result does not regard undefined as an undeclared variable. Otherwise, a runtime error occurs.
Alert (typeof (a); // result undefined alert (A = undefined); // result true alert (A === undefined) // result ture, the consistency operator returns true only when both types and values are equal.

We do not recommend that you use the IF (variable = undefined) method for value-free verification, because the default values of null and undefined in JavaScript are equal.

  alert(null == undefined);   //true

It is best to use the consistency operator or typeof to determine

If (variable = undefined) // or if (typeof (variable) = "undefined ")

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.