What are the basic types of JS? What are the reference types? (ii) Differences between------null and undefined

Source: Internet
Author: User

The difference between null and undefined

1. Types of differences

Console.log (typeof (undefined));//undefined
Console.log (typeof (null));//object
Console.log (null==undefined);//true
Console.log (null===undefined);//false

------------extension, why Console.log (typeof (null));//object-----This is because ES is designed to assume that null represents an empty object pointer, except that the value is empty, but the code executes properly. So when using typeof, it returns "Object". Therefore, NULL is generally used as a placeholder for an empty object.

2.null can be converted to 0 when the original design is a type of reload, while the undefined is n

Console.log (number (null));//0

Console.log (number (undefined));//nan

3.undefined and Null will be automatically converted to false in the IF statement;

    if (! NULL ) {        //dosomething    }    if(!  Undefined) {        //dosomething    }

4. Current usage

At present, null and undefined are basically synonymous, with only a few subtle differences.

Null means "No object", that is, there should be no value at all. 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.

Object.getprototypeof (object.prototype)//null

Undefined means "missing value", that is, there should be a value here, but there is no definition. 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 i; IUndefinedfunctionF(x){Console.log (X}f ( //Undefinedvar o = new object (;o . P//Undefinedvar x = f () ;x//undefined 

This article refers to the link: http://www.ruanyifeng.com/blog/2014/03/undefined-vs-null.html

What are the basic types of JS? What are the reference types? (ii) Differences between------null and 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.