What are null, undefined and NaN?
Try code in the playgroundUnderstand the concept
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.
Important ideas
-
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.
Can you explain it without the notes?
What are null, undefined and NaN?
Use your own words. Explain an example and one limitation before checking the explanation.
Check your explanation
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.
Turn this concept into a clear answer.
- 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?”. Examples are study fragments; framework behavior can depend on your version.