Differences between Null and Undefined in JS --, jsnullundefined

Source: Internet
Author: User

Differences between Null and Undefined in JS --, jsnullundefined

There are two primitive types in JavaScript: Null and Undefined. These two types often make JavaScript developers confused, when is it Null, and when is it Undefined?

The Undefined type has only one value, that is, undefined. When the declared variable has not been initialized, the default value of the variable is undefined.
The Null type also has only one value, that is, null. Null indicates an existing object. It is often used to indicate that a function attempts to return a non-existing object.

Js Code
  1. VarOValue;
  2. Alert (oValue = undefined); // output "true"


This code is displayed as true, indicating that the value of oVlaue is undefined because it is not initialized.

Js Code
  1. Alert (Null= Document. getElementById ('notexistelement '));


When a DOM node with the id "notExistElement" does not exist on the page, this code is displayed as "true" because we try to obtain a nonexistent object.

Js Code
  1. Alert (TypeofUndefined); // output "undefined"
  2. Alert (Typeof Null); // Output "object"


The first line of code is easy to understand. The undefined type is Undefined. The second line of code is confusing. Why is the null type an Object? In fact, this was an error originally implemented by JavaScript and was subsequently followed by ECMAScript. Today, we can interpret null as a placeholder for a non-existent object, but pay attention to this feature in actual encoding.

Js Code
  1. Alert (Null= Undefined); // output "true"


ECMAScript considers undefined to be derived from null, so they are defined as equal. However, in some cases, we must differentiate these two values. What should we do? You can use the following two methods.

Js Code
  1. Alert (Null=== Undefined); // output "false"
  2. Alert (Typeof Null=TypeofUndefined); // output "false"


The typeof method has already been used. The type of null is different from that of undefined, so the output is "false ". ===Indicates that the value is absolutely equal. Here, null ===undefined outputs false.

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.