Undefined
Indicates that the variable has been declared but has not been initialized. It should be noted that when using the typeof operator to determine the data type, the values returned by undeclared variables and uninitialized variables are undefined.
var message;console.log(typeof message);//undefinedconsole.log(typeof age);//undefined
null: represents an empty object pointer
The value returned by using the typeof operator is 'object'. It should be noted that the undefined value is derived from a null value, so ECMA-262 stipulates that they must return true when testing for equality.
console.log(null == undefined);//true
The difference between undefined and null
(1) undefined means that a variable has not been declared, or has been declared but has not been assigned
(2) null is a value that indicates "no value"
(3) Javascript sets the default value of unassigned variables to undefined
(4) Javascript never sets variables to null. It is used to let the programmer show that a variable declared with var has no value.
(5) undefined is not a valid JSON, while null is
(6) The type of undefined is undefined, and the type of null is object.
PS: They are all basic types
They are all false (Boolean(undefined) // false, Boolean(null) // false)
You can determine whether a variable is undefined in this way
typeof variable === "undefined"
You can determine whether a variable is null in this way
variable === null
They are equal when compared with double equal signs, but they are not equal when compared with three equal signs.
null == undefined // true null === undefined // false