What are null, undefined and NaN?
Practice this questionWANT TO UNDERSTAND THE CONCEPT FIRST?Learn What are null, undefined and NaN?
Definition:
undefined usually means a value has not been supplied, while null is commonly used to mark an intentionally empty value. NaN means a numeric calculation or conversion did not produce a valid number. These are different cases, so an API should not treat them as interchangeable without a clear rule.
Key points:
-
null: intentionally empty.undefined: missing or not assigned. -
NaN: a number value representing an invalid numeric result. -
Use
Number.isNaN(value)to check forNaN. -
A missing object property evaluates to
undefined; a JSON response can explicitly containnull. -
typeof NaNis"number"; the name describes a special number value, not a separate JavaScript type.
Interview point:
- Use
value ?? fallbackwhen only null and undefined should trigger a default;||also replaces valid values such as 0 and an empty string.
Adapted from 01_JAVASCRIPT_TYPESCRIPT.md · “What are null, undefined and NaN?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.