Glossary / null

null

null is a primitive value commonly used to express an intentional absence of an object or result. JavaScript does not enforce one universal convention: APIs may use null to mean “known to be empty,” while undefined may mean “not supplied” or “not found.” Consistent domain contracts are more important than the particular convention.

Distinguishing null from undefined

const profile = { nickname: null };
profile.nickname; // null: explicitly present with no nickname
profile.missing;  // undefined: no such property

typeof null returns "object", a historic bug retained for web compatibility. Test null with value === null. Loose equality treats null and undefined as equal to each other and unequal to other values; strict equality distinguishes them.

Nullish coalescing (??) and optional chaining (?.) treat null and undefined alike. Default parameters and destructuring defaults only respond to undefined. Truthiness-based fallback (||) also replaces values such as zero, false, and empty string, which are often valid.

const limit = suppliedLimit ?? 20; // Preserves 0
const label = suppliedLabel || "Untitled"; // Replaces ""

Whether these choices are correct depends on the domain. An empty string might mean a user intentionally cleared a label; zero can be a meaningful limit.

API and data-model decisions

Use null when an explicit empty state is meaningful in serialized data or API contracts; use omission when the field was not included or should be left unchanged. For partial updates, define whether null means clear, undefined means ignore, and absence means unchanged. JSON represents null, but cannot represent undefined directly. Validate incoming data instead of letting nullability be accidental.

Interview questions

  1. What is typeof null? "object", due to a historical compatibility quirk.
  2. How should null be tested? value === null.
  3. Do ?? and || behave the same for null? Both use the fallback for null, but || also falls back for all other falsy values.
  4. Why distinguish null from a missing field? Presence can carry domain meaning, especially in patch requests and configuration.

References