🧩 1. What is undefined?
let a;
console.log(a); // undefined
✅ Meaning
A variable is declared in memory but not assigned a value
🔬 Behind the scenes
During creation phase:
a → undefined
So:
Variable exists in memory
Value is default-initialized to undefined
❌ 2. What is undeclared?
console.log(b); // ReferenceError
❌ Meaning
Variable was never declared at all
🔬 Behind the scenes
b → ❌ not present in memory
👉 Engine cannot find it → throws ReferenceError
⚖️ Key Difference
⚙️ 3. What is typeof?
typeof is an operator that returns the type of a value as a string.
Examples
typeof 10 // "number"
typeof "hello" // "string"
typeof true // "boolean"
typeof undefined // "undefined"
🔥 Special Case (Important)
typeof b // "undefined"
👉 Even though b is undeclared!
❗ Why does typeof not throw error?
Normally:
b // ❌ ReferenceError
But:
typeof b // "undefined" ✅
🧠 4. Why typeof undefined and typeof undeclared are same?
Because:
typeof undeclared → "undefined"
typeof undefined → "undefined"
👉 But internally they are NOT the same thing
🔬 Internal Reason
For declared variable:
let a;
typeof a // "undefined"
👉 Engine:
Find a → value is undefined → return "undefined"
For undeclared variable:
typeof b
👉 Engine does something special:
Check if variable exists
IF NOT → return "undefined" instead of throwing error
🛡️ 5. Safety Guard Feature of typeof
This is intentional design in JavaScript.
🎯 Purpose
Allow safe checks for variables that may not exist
✅ Example
if (typeof someVar !== "undefined") {
console.log("exists");
}
👉 Safe even if someVar is never declared
❌ Without typeof
if (someVar !== undefined) {
// ❌ ReferenceError
}
🔬 Internal Behavior (Spec-Level Concept)
Normally variable access:
ResolveBinding(name)
If not found:
→ ReferenceError
But typeof does:
If binding not found → return "undefined"
👉 Special handling in spec
⚡ Real-World Use Case
Feature detection
if (typeof window !== "undefined") {
// browser environment
}
👉 Used in frameworks like Next.js
⚠️ Important Edge Case
typeof null // "object" ❌
👉 Historical bug in JavaScript
🧠 Mental Model
Think like this:
Normal access → strict (error if not found)
typeof → safe (never throws for variables)
🎯 Final Takeaways
undefined → declared but no value
undeclared → not defined at all
typeof:
returns type as string
never throws error for undeclared variables
acts as a safety guard
🔥 Interview-Level Answer
typeof is a safe operator in JavaScript that returns the type of a value and uniquely does not throw a ReferenceError when used on undeclared variables, instead returning "undefined".

Top comments (0)