JavaScriptInterview preparation
Foundation·Concept·2 min read

What are null, undefined and NaN?

Practice this question

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 for NaN.

  • A missing object property evaluates to undefined; a JSON response can explicitly contain null.

  • typeof NaN is "number"; the name describes a special number value, not a separate JavaScript type.

Interview point:

  • Use value ?? fallback when 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.