
π§© 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".
United States
NORTH AMERICA
Related News
What Does "Building in Public" Actually Mean in 2026?
19h ago
The Agentic Headless Backend: What Vibe Coders Still Need After the UI Is Done
19h ago
Why Iβm Still Learning to Code Even With AI
21h ago
I gave Claude a persistent memory for $0/month using Cloudflare
1d ago
NYT: 'Meta's Embrace of AI Is Making Its Employees Miserable'
1d ago
