Interview answer drill

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

undefined vs not defined in JavaScriptFrontend interview answer

HighIntermediateJavascript
Interview focus

This JavaScript interview question tests whether you can explain undefined vs not defined in JavaScript: TDZ, typeof, and debug order, connect it to production trade-offs, and handle common follow-up questions.

  • undefined vs not defined in JavaScript: TDZ, typeof, and debug order explanation without falling back to memorized docs wording
  • Undefined and Referenceerror 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 is a value state; not defined is identifier lookup failure. Strong debugging answers also separate those from TDZ and null, then show the safe check order for missing globals, payload fields, and scope mistakes.

Full interview answer

One-liner

undefined means "found variable, value missing". "not defined" means "variable name could not be found".

Real debugging version
A missing API field often becomes undefined. A missing global, typo, or script-load failure often throws "is not defined" instead.

Case

Declared?

Outcome

let x; console.log(x)

Yes

undefined

console.log(nope)

No

ReferenceError: nope is not defined

TDZ access (let/const before line)

Yes (uninitialized)

ReferenceError

"Not defined" and TDZ both throw, but for different reasons.
JAVASCRIPT
let a;
console.log(a); // undefined

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

{
  // console.log(c); // ReferenceError (TDZ, not "undeclared")
  let c = 1;
}
                  

typeof nuance

typeof is safe for an undeclared identifier, but it still throws inside TDZ. That is the follow-up distinction many answers skip.

JAVASCRIPT
console.log(typeof maybeMissing); // 'undefined' (safe for undeclared names)

{
  // console.log(typeof inTdz); // ReferenceError
  let inTdz = 1;
}
                  

Real debugging example
A missing property and a missing identifier are different failures. One is data state; the other is name resolution.

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

console.log(typeof AppConfig); // 'undefined' if the global never loaded
// console.log(AppConfig.timeout); // ReferenceError if AppConfig is undeclared
                  

Fast check order

  • If the error literally says "is not defined", check typo, scope, or load order first.
  • If a read returns undefined, inspect assignment timing or object shape.
  • If typeof itself throws, you are likely in TDZ rather than dealing with an undeclared global.
  • null is different again: the value is intentionally empty, not missing by accident.

Symptom

Likely cause

is not defined

Typo, wrong scope, or script/module load-order issue.

Cannot access X before initialization

TDZ with let/const/class.

Value is undefined unexpectedly

Missing assignment/data path/property.

Use error text to jump directly to root cause.

Practical scenario
A feature flag name is misspelled in one module and unset in another. One path throws "not defined", another returns undefined. Distinguishing these quickly saves debugging time.

Still so complicated?

undefined is an empty seat with a seat number. "not defined" means there is no seat with that number in this hall.

Summary
  • undefined is data state.
  • not defined is scope lookup failure.
  • TDZ is a third case: declared but temporarily inaccessible.
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.