Excerpted from: WeChat public account: Front-end Daquan
simple distinction
In general null
, and undefined
both represent null, the main difference is that undefined
it represents the value of a variable that has not been initialized, and null
that the variable intentionally lacks an object pointer.
-
undefined
-
This variable is fundamentally undefined
-
hidden null
-
-
null
-
Although the value is defined, it does not point to any object in memory
-
declarative null
-
Definition given in MDN
null
The value null
is a literal, unlike undefined
, it's not a property of the global object. null
is a missing flag indicating that the variable does not point to any object. It may be better understood null
as an object that has not been created yet. In APIs, it is null
often used where the return type should be an object, but there is no associated value.
undefined
undefined
is a property of the global object . That is, it is a variable in the global scope. undefined
The initial value of is the primitive data type undefined
.
an amazing picture
Next, let's look at a more classic picture. This picture comes from the answer of stackoverflow. I have not found the exact source.
Manifestations
Before understanding null
the undefined
difference between He and He, we must first know what are the different forms of He null
and He undefined
in JS
order to facilitate our better understanding null
of undefined
the difference between He and He.
typeof
typeof null // 'object'
typeof undefined // 'undefined'
Object.prototype.toString.call
typeof null // '[object Null]'
typeof undefined // '[object Undefined]'
== and ===
null == undefined // true
null === undefined // false
!!null === !!undefined // true
Object.getPrototypeOf(Object.prototype)
JavaScript
The prototype of the first object in null
.
Object.getPrototypeOf(Object.prototype) // null
+ operation and Number()
let a = undefined + 1 // NaN
let b = null + 1 // 1
Number(undefined) // NaN
Number(null) // 0
JSON
JSON.stringify({a: undefined}) // '{}'
JSON.stringify({b: null}) // '{b: null}'
JSON.stringify({a: undefined, b: null}) // '{b: null}'
let undefiend = 'test'
function test(n) {
let undefined = 'test'
return n === undefined
}
test() // false
test(undefined) // false
test('test') // ture
let undefined = 'test' // Uncaught SyntaxError: Identifier 'undefined' has already been declared
Explore in depth
Why typeof null is object?
typeof null
The output is 'object'
actually a low-level bug that cannot be fixed until now.
The reason is that, in the JavaScript
initial version, the value is 32位
stored in . The front 3位
is a flag indicating the data type, and the remaining bits are the value.
For all objects, it is preceded 3位
by 000
the type flag bit. In JavaScript
earlier versions, it null
was considered a special value and was used to correspond C
to 空指针
. But there JavaScript
is no C
pointer in , so it means null
nothing or nothing .void
全0(32个)
So whenever it's JavaScript
read , it's treated as if by the null
frontend , which is why it 's returned .3位
对象类型
typeof null
'object'
Why does Object.prototype.toString.call(null) output '[object Null]'
toString()
Is Object
the prototype method, calling this method returns the current object's by default [[Class]]
. This is an internal property of the form [object Xxx]
where Xxx
is the type of the object.
Everything in JavaScript is an object, why can't xxx.toString() return the variable type?
toString
This is because the methods overridden in each class need to be called, and the methods must be called Object
in the same way.toString
toString.call()
For Object
objects, a direct call toString()
will return '[object Object]'
. For other objects, it needs to call / apply
be called to return the correct type information.
Why does == and === compare true and false?
A lot of articles say: undefined
The boolean of is false
, so is null
the boolean of false
, so they are both converted when comparing false
, so undefined == null
.
Actually it is not so.
ECMA
It tells us clearly in the 11.9.3
chapter:
If x is null and y is undefined, return true.
If x is undefined and y is null, return true.
This is the JavaScript
bottom-level content. As for the more in-depth content, if you are interested, you can take a look JavaScript
at the source code.
Why is it different null + 1
from undefined + 1
performance?
This involves JavaScript
implicit type conversions in , 加法运算
which attempt to convert the variables in the expression to types before executing them number
. Such as: '1' + 1
will get the result 11
.
-
null
number
When converted to , will convert to0
-
undefined
number
When converted to , it converts toNaN
As to why the conversion is performed like this, I'm guessing it was JavaScript
a bad design early on.
From a linguistic point of view: null
means a definite unpointed null value, while undefined
it means an unknown value.
In a way, 0
means numeric nulls.
This may seem far-fetched, but it's the best I can think of at this stage.
Why does JSON.stringify remove content with value undefined?
In fact, there is no good explanation for this, and the corresponding key JSON
will undefined
be deleted, which is JSON
its own conversion principle.
In undefined
the case of , there is no difference in the presence or absence of this piece of data, because they are not different in terms of representation:
let obj1 = { a: undefined }
let obj2 = {}
console.log(obj1.a) // undefined
console.log(obj2.a) // undefined
But it should be noted that you may need to perform special processing on JSON
the data in the format when calling the interface .undefied
Why let undefiend = 'test' override JavaScript's own undefined?
JavaScript
The undefined
restriction method for creates a read-only for the global undefined
, but does not completely prohibit undefined
the definition of local variables.
It is said that JavaScript
this operation is prohibited in the high version, but I have no accurate basis.
Please do not undefined
overwrite variables at any time, even if your JSON
conversion will undefined
convert to ''
. Do not go through this operation either, it would be extremely dangerous behavior.
Summarize
About using undefined or null
This is a controversial content of the public saying that the public is reasonable and the mother is reasonable.
I prefer to use null
because that's how nulls are explicitly defined. I can't give an exact reason.
But undefined
I have a piece of advice about using:
if you need to use undefined
define null, don't do either of the following:
-
let a;
-
let a = undefined;
And then explicitly declare it in the following way undefined
:
-
let a = void 0;