Interview answer drill

Use this JavaScript interview question to rehearse a quick answer, common mistake, follow-up, and production pitfall.

What is the difference between a variable that is: null, undefined or undeclared?Frontend interview answer

HighEasyJavascript
Interview focus

This JavaScript interview question tests whether you can explain null vs undefined vs undeclared in JavaScript, connect it to production trade-offs, and handle common follow-up questions.

  • null vs undefined vs undeclared in JavaScript explanation without falling back to memorized docs wording
  • Null and Undefined reasoning, edge cases, and production failure modes
  • How you would answer the most likely JavaScript interview follow-up
Practice more JavaScript interview questions
Interview quick answer

undefined means declared but currently no value, null means intentionally empty, and undeclared means the identifier does not exist in scope. Treat them differently in logic and debugging.

Full interview answer

Direct distinction

undefined: declared, no assigned value yet.
null: explicitly set to no value.
undeclared: name does not exist in current scope.

State

Example

Read result

undefined

let x; console.log(x)

undefined

null

let user = null; console.log(user)

null

undeclared

console.log(missingVar)

ReferenceError

Same "empty-ish" feeling, very different runtime meaning.
JAVASCRIPT
let a;
console.log(a); // undefined

let b = null;
console.log(b); // null

// console.log(c); // ReferenceError: c is not defined
                  

Where undefined appears naturally

JAVASCRIPT
const obj = {};
console.log(obj.age); // undefined (missing property)

function greet(name) {
  console.log(name); // undefined when omitted
}
greet();
                  
JAVASCRIPT
console.log(undefined == null);  // true
console.log(undefined === null); // false
console.log(typeof null);        // 'object' (historical JS quirk)
                  

Practical rule
Use null when you intentionally clear/reset a value. Treat undefined as "not provided yet". Treat undeclared as a bug (scope/typo/load-order).

Common pitfalls

  • Using truthy checks and accidentally treating 0/false/'' as missing.
  • Assuming typeof null should be 'null'.
  • Catching undeclared errors instead of fixing declaration/scope.

Still so complicated?

undefined = empty box that exists. null = box intentionally marked "empty". undeclared = no box at all.

Summary
  • undefined is a value.
  • null is an intentional value choice.
  • undeclared is usually a coding error.
  • Use strict equality when comparing these states.
Similar questions
Guides
Preparing for interviews?

Use this as one explanation rep, then continue with the JavaScript interview questions cluster or a guided prep path.