JavaScript advanced Programming--an analysis of the difference between undefined and null

Source: Internet
Author: User

An analysis of the difference between undefined and null Similarities:

1, undefined, and null are the basic data types of JavaScript. In fact, there is not much difference between the two.

Defined:

var A=undefined;var a=null;null==undefined;//true
2, NULL is an object that represents "none", and a value of 0;undefined is a primitive value that represents "none", and a value of Nan when converted to a number.

Number (undefined)//nan5 + Undefined//nan
and for NULL

Number (NULL)//+ null//5

difference 1, null means "no object", that is, there should be no value at all. Typical use methods are:
(1) As a function's parameter, the function's parameters are not objects. (2) As the end point of the object prototype chain.


Object.getprototypeof (object.prototype)//null

2. Undefined means "missing value". This is where there should be a value, but not yet defined. Typical use methods are:
(1) The variable is declared. But when there is no value assigned. is equal to undefined. (2) The number of parameters that should be provided when calling a function is not provided. This number 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.

Example

var a;a;//undefinedfunction f (x) {console.log (x);} f (x);//undefinedvar obj=new Object (); Obj.name;//undefinedvar x=f (); x;//undefined


JavaScript advanced Programming--an analysis of the difference between undefined and null

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.