LearnThatStack Ace your next interview
JavaScript · question
Question 6 of 126

What is the difference between null and undefined?

beginner
← All JavaScript questions
Re-explain

undefined means a value was never assigned, while null is an explicit "no value". JavaScript produces undefined on its own, and null comes from your code or from APIs such as document.getElementById() when nothing matches.

A variable that is declared but not assigned is undefined, and so is a function parameter the caller did not provide. A missing object property and a function with no return also give undefined. null is an intentional assignment that marks a value as empty.

The two are loosely equal but not strictly equal, so null == undefined is true and null === undefined is false. typeof undefined is "undefined", but typeof null is "object", a legacy quirk.

let a; // undefined
let b = null; // null
typeof undefined; // "undefined"
typeof null;      // "object" (legacy quirk)
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for JavaScript One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • When would you set a value to null on purpose instead of leaving it undefined? Name a real case, like an API field that exists but is empty or a reference you clear, and say why a deliberate blank helps the next reader.
  • How do you check for null or undefined in one expression? Mention x == null, and ?? versus ||, since || also replaces 0, '' and false.
  • How do you tell a property set to undefined apart from a property that doesn't exist? Reading it gives undefined both ways. A strong answer checks whether the key exists with the in operator or Object.hasOwn.
  • What does a default parameter do when the caller passes null instead of undefined? Defaults apply only for undefined, so an explicit null gets through unchanged. Say what that means for callers who pass null to mean 'use the default'.
  • What does JSON.stringify do with a property whose value is undefined, and with one that is null? Keys holding undefined are left out of the output, and null is written as null. Say why that matters when an API reads null as 'clear this field'.

What you can say

  1. undefined means nothing was ever assigned, and null means someone set the value to empty on purpose.
  2. JavaScript produces undefined by itself, for a declared variable with no value, a parameter the caller skipped, a missing property, or a function that returns nothing.
  3. null only shows up when code puts it there, either your code or an API, like document.getElementById returning null when nothing matches.
  4. They're loosely equal, so null == undefined is true, but null === undefined is false.
  5. One trap is typeof, which gives 'undefined' for undefined but 'object' for null, a legacy quirk, so I test for null with === null instead.

Weak answers to avoid

  • Treats them as interchangeable They are only loosely equal. A check like x === null misses undefined. Say where each one comes from, then how == and === treat the pair.
  • Says typeof null returns 'null' It returns 'object', a legacy quirk. Getting this wrong suggests you've never tried it. Name the quirk and check for null with x === null.
  • Confuses undefined with undeclared Reading an undeclared variable throws a ReferenceError. A declared variable with no value reads as undefined. Keep the two cases separate.
  • Defines both but never says where they come from Interviewers want the cases. Name them: an unassigned variable, a skipped argument, a missing property, a function with no return, and null from APIs like getElementById.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the JavaScript cheatsheet.

← Back to all JavaScript questions
Pro · $10/mo

110 of 126 JavaScript answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime