The difference between JavaScript null and undefined

Source: Internet
Author: User

Undefined means "missing value", that is, there should be a value here, but it has not been assigned a value. Typical uses are:

(1) The variable is declared, but when it is not assigned, it is equal to undefined.

(2) when calling a function, the argument that should be supplied is not provided, which equals undefined.

(3) The object does not have an assigned property, and the value of this property is undefined.

(4) When the function does not return a value, undefined is returned by default.

var Ovalue;   // output "true"  

Null is used to represent an object that does not already exist, and is commonly used to indicate that a function is attempting to return a nonexistent object. Typical uses are:

(1) As a function parameter, the parameter of the function is not an object.

(2) As the end point of the object prototype chain.

Alert (null = = document.getElementById (' notexistelement '));  
Alert (typeof undefined);   // output "undefined"   Alert (typeofnull);        // output "Object"  
Alert (null = = undefined);   // output "true"  

ECMAScript that undefined are derived from null, so they are defined as equal.

Alert (null//Output "false"  alert (typeofnulltypeof   Output "false"  

Null is not the same as the type of undefined, so the output is "false". and ' = = = ' stands for absolute equals, where null = = = undefined output false.

The difference between JavaScript null and undefined

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.