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