What is the difference between null and undefined?

UndefinedThe type has only one value, which is undefined. When the declared variable has not been initialized, the default value of the variable is undefined.
NullThe type also has only one value, which is null. Null is used to indicate an object that does not yet exist, and is often used to indicate that the function attempts to return an object that does not exist.

Same point:

Are all primitive values, stored locally in the stack variable

The difference between the two:

1. The type is different:

console.log(typeOf undefined);//undefined
console.log(typeOf null);//object

2. Not the same when converted to value: undefined is NaN, null is 0

console.log(Number(undefined));//NaN
console.log(Number(10+undefined));//NaN
console.log(Number(null));//0
console.log(Number(10+null));//10

3.undefined===null;//false

  undefined==null;//true

When to use:
null When you have finished using a relatively large object and need to release it, set it to null;

 

Published 203 original articles · praised 8 · 10,000+ views

Guess you like

Origin blog.csdn.net/z591102/article/details/105533293