Skip to solution
easyFrontend

What is the difference between null and undefined?

584 views
01

Understand the problem

Question presented to candidate: "What's the difference between null and undefined, and when would you deliberately choose to use one over the other in your own code?"

What a strong answer should cover:

  • 📌 Interview term: undefined — the value JavaScript automatically assigns to a variable that has been declared but not yet given a value, to a missing function argument, or to accessing a property that does not exist on an object. It represents an implicit, unintentional absence.
  • 📌 Interview term: null — a value a developer explicitly assigns to represent "no value" or "empty" on purpose. It is an intentional absence, set deliberately by code, not automatically by the engine.
  • 📌 Interview term: the famous typeof null bugtypeof null returns "object", a historical bug preserved for backward compatibility since fixing it would break the web. Verified directly: typeof undefined correctly returns "undefined", but typeof null does not return "null".
  • A precise answer names null + 1 (which is 1, since Number(null) is 0) versus undefined + 1 (which is NaN, since Number(undefined) is NaN) — verified directly, a real, sharp distinction in numeric coercion.
  • A precise answer names JSON.stringify behavior: a property with value undefined is genuinely dropped from the output entirely, while a property with value null is kept — verified directly. This is a real, practical reason APIs often use null over undefined for "explicitly no value" fields.

Clarifying questions expected:

  • None — this is a definitional/comparison question; naming the intentional-vs-implicit distinction plus the coercion/serialization differences is the strong signal.

Code / implementation expected: Optional — showing the typeof and JSON.stringify contrasts directly demonstrates real understanding.

nullundefined
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals interviews. Difficulty: Easy

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every check below was actually run in Node.

1. Why This Even Matters — A Story First

T

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal typeof, coercion, and JSON.stringify differences between null and undefined
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 19 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track