The difference between null and undefined

Sameness:

In JavaScript, null and undefined are almost equal

In the if statement, null and undefined will be converted to false. Both are equal when compared with the equality operator.

console.log(null==undefined);    //true  因为两者都默认转换成了false
console.log(typeof undefined);    //"undefined"  
console.log(typeof null);       //"object"  
console.log(null===undefined);    //false   "==="表示绝对相等,null和undefined类型是不一样的,所以输出“false”

The difference between null and undefined:

Null means that there is no object, and an object may be assigned in the future, that is, there should be no value

1) As a function parameter, it means that the function parameter is not an object

2) As the end of the object prototype chain


Object.getPrototypeOf(Object.prototype)
// null

undefined means missing value, that is, there should be a value here, but it is not defined

1) If the formal parameters are defined, the actual parameters are not passed, and undefined is displayed

2) When the object property name does not exist, undefined is displayed

3) The function does not write the return value, that is, the return is not written, and the result is undefined

4) I wrote return, but did not assign a value, I got undefined


var i;
i // undefined

function f(x){
    
    console.log(x)}
f() // undefined

var  o = new Object();
o.p // undefined

var x = f();
x // undefined

Guess you like

Origin blog.csdn.net/weixin_43638968/article/details/109292296