Null & Undefined
easyComplete guide with examples and interview questions · 20 min
Say it out loud in an interview — clear, simple, no textbook wording.
If someone asks me about null vs undefined, I'd say they are JavaScript's two flavors of "nothing" — and they mean different things. null is a decision: I assigned it to mean "there is intentionally no value here," like a user lookup that found no user. undefined is a default state: JavaScript's way of saying "not set yet" — a declared variable you never assigned, a function argument nobody passed, a property that doesn't exist, or a function that returned nothing.
Interviewers care about this topic because it's a trap factory. typeof null says 'object' — a 1995 bug that can never be fixed without breaking the web. null == undefined is true but null === undefined is false. Number(null) is 0 while Number(undefined) is NaN. And the default-value question separates juniors from seniors: || falls back on any falsy value, while ?? only falls back on null and undefined.
Senior rounds sometimes probe the typeof guard: typeof someVar === 'undefined' doesn't throw even when someVar was never declared, which is why old code used it to feature-detect globals before optional chaining existed.
Key takeaways
- •null is an intentional 'no value' you assign; undefined is JavaScript's default for 'not set yet'
- •typeof null returns 'object' — a 1995 bug kept for backwards compatibility, never check null with typeof
- •null == undefined is true; null === undefined is false — the one == most style guides allow
- •Use ?? for defaults instead of || so you don't clobber 0, '', or false
- •typeof x === 'undefined' is safe on undeclared variables; x === undefined throws a ReferenceError
Track your mastery
Sign in to save Q&A scores, coding challenge progress, points, and streaks across devices.
Sign in to save progress