A brief overview of null and undefined
13 сентября 2025 г.
Null is a reserved keyword in the language. It usually represents the intentional absence of a value; typeof null returns "object".
Undefined also represents the absence of a value, but in a more implicit or “deeper” sense; typeof undefined returns "undefined".
Undefined appears in several cases:
- A predefined global constant (initially set to undefined)
- The value of variables that have not been initialized
- Returned when requesting the value of an object or an array method that does not exist
- Returned by a function when no return value is specified
- The value of a function parameter that was not provided
Comparison
- Loose equality (==) treats null and undefined as equal
- Strict equality (===) treats them as not equal
Finally
undefined is generally considered an accidental or implicit absence of a value (often treated like an error or missing initialization), while null is an intentional assignment meaning “no value.”