JavaScript null Primitive
A null primitive represents the intended absence of a value.
The type name is object (A JavaScript mistake).
Null values are one of JavaScript's 7 primitive types.
Type Information
The primitive type represented by null is called null.
However, JavaScript reports object when using typeof.
Note
This is a historical JavaScript behavior.
Null is a primitive value, not an object.
Think of it as a JavaScript design error.
The null Value
JavaScript can only hold one value:
null
Null is often used to indicate that a variable should contain no value.
Assigning Null
Falsy Value
Null is a falsy value.
Null Is Not an Object
Even though typeof null returns "object", null is not an object.
Using Null to Clear References
Null is often used to remove a reference to an object.
Example
let person = {name:"John"};
person = null;
After the assignment, the variable no longer refers to the object.
Nullish Coalescing
The nullish coalescing operator (??) treats null and undefined as missing values.
Summary
| Question | Answer |
|---|---|
| Primitive Type | null |
| Possible Values | Only 1 (null) |
| False | Yes |
| Object | No |
| typeof | "object" |