When I first started learning JavaScript
I felt that undefined was quite new
Because other languages don’t have
and not only undefined, but also a null that means empty
I believe many students can’t tell the difference between undefined and null.
Today I will summarize the difference between undefined and null, which means empty in JavaScript.
undefined and null are both JavaScript The basic types in
and they have only one type value each, which are undefined and null, which have the same names as their types
Normally, there is really no difference between these two values
var foo = undefined;
var foo = null;
And the standard also has such a provision
console.log(null == undefined) // true
(but null !== undefined
)
is converted into a Boolean value and is false
if(!undefined){ console.log(1);//1}if(!null){ console.log(1);//1}
But in fact, there is a difference
Let’s take a look at these lines of code first
console.log(undefined in window);// trueconsole.log(null in window);// false
This is the difference
null is the keyword of JavaScript
and undefined is window A special property on (the same as NaN and Infinity)
Although ES5undefined becomes a read-only property, there are still some unreasonable things, such as this
function fn(){ var undefined = 1;// 注意:前方高能 console.log(undefined);// 1} fn();
Look, because undefined is not the key words, so we can declare undefined in local variables (simply speechless)
Even if you are in strict mode
function fn(){ "use strict"; var undefined = 1; console.log(undefined);// 1} fn();
We must not write such outrageous code
In progress There are also differences in mathematical operations
console.log(undefined + 1); //NaNconsole.log(null + 1); //1
Any operation performed on undefined will be converted to NaN
and null is converted to 0
JavaScript has two values that represent null It makes sense
The father of js, Brendan Eich, originally designed it like Java, with only null
But he considered that js has two basic values and reference values
Using an object to represent empty monsters typeof null === 'object'
, and null is converted into the number 0, resulting in many bugs that are difficult to find
So he designed undefined
According to the design of the father of js
undefined means a missing value
null means an empty object
But we don’t use it like this now
It can be understood this way
undefined means the variable is not initialized , no value
null means the variable has been initialized, and the value is "null value"
undefined is used by js, and null is for us
I don’t know if you can understand this sentence
For example, in these cases
var a;
declare a variable, the engine will assign it a default value of undefined
#function a(){} Declare a function, if there is no return value, it will return undefined by default
var obj = {};obj.a; Access non-existent object properties, the default value is undefined
var a = function(a){};a(); No actual parameters are specified, The default parameter value is undefined
used by js and we To use null
For example
var obj = Object.create(null); Create an empty object
demo.onclick = null; Unbind event