Difference between JS- null and undefined

null

The Null type has only one value, the special value null. A null value represents a null object pointer, which is why typeof passing in a null returns "object".

undefined

Both null and undefined can mean "nothing", and the meanings are very similar. Assign a variable to undefined or null

relationship between the two

The equality operator (==) even reports equality directly. Both are false in the if judgment

console.log(null == undefined);    // true

When defining variables that will hold object values ​​in the future, it is recommended to initialize them with null. This can further distinguish between null and undefined.
Null is an object representing "empty", which is 0 when converted to a value; undefined is a primitive value representing "no definition here", and is NaN when converted to a value.

let message = null
let age
console.log(message);    // null
console.log(age);        // undefined
console.log(Number(message));    // 0
console.log(Number(age));    //undefined

Guess you like

Origin blog.csdn.net/qq_45859670/article/details/124498124