Null and undefined

Different states, undefined is a kind of primitive value, non-human operation; Null is empty;

Undefined literally means undefined value. The semantics of this value are intended to represent the original state of a variable, not the result of a human operation. This primitive state can occur in four scenarios:

  1. A variable is declared, but no value is assigned
  2. Access properties that do not exist on an object
  3. The function defines parameters but passes no arguments
  4. Evaluate an expression using void

Therefore, undefined usually comes from the original state value of an expression, not the result of human manipulation. Of course, you can assign undefined to a variable manually, but that doesn’t make sense because a variable that isn’t assigned is undefined.

The literal meaning of null is null. The semantics of this value are intended to indicate that an object has been artificially reset to an empty object, rather than the original state of a variable. The in-memory representation is that a variable in the stack does not refer to an in-memory object in the heap

Null has its own type, null, which is not Object. The reason why TypeOF is considered Object is that JavaScript data types are represented in binary form at the bottom. The first three digits of binary are 0, which is considered as Object type by Typeof. The binary bits of NULL are all zeros, so null is mistakenly identified as Object.