DEV Community

Ashish Ghildiyal
Ashish Ghildiyal

Posted on

undefined vs undeclared, and how typeof behaves

🧩 1. What is undefined?

let a;
console.log(a); // undefined
Enter fullscreen mode Exit fullscreen mode

✅ Meaning
A variable is declared in memory but not assigned a value

🔬 Behind the scenes

During creation phase:

a → undefined
Enter fullscreen mode Exit fullscreen mode

So:
Variable exists in memory
Value is default-initialized to undefined

❌ 2. What is undeclared?

console.log(b); // ReferenceError
Enter fullscreen mode Exit fullscreen mode

❌ Meaning
Variable was never declared at all

🔬 Behind the scenes

b → ❌ not present in memory
Enter fullscreen mode Exit fullscreen mode

👉 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"
Enter fullscreen mode Exit fullscreen mode

🔥 Special Case (Important)

typeof b // "undefined"
Enter fullscreen mode Exit fullscreen mode

👉 Even though b is undeclared!

Why does typeof not throw error?

Normally:

b // ❌ ReferenceError
Enter fullscreen mode Exit fullscreen mode

But:

typeof b // "undefined" ✅
Enter fullscreen mode Exit fullscreen mode

🧠 4. Why typeof undefined and typeof undeclared are same?
Because:

typeof undeclared → "undefined"
typeof undefined  → "undefined"
Enter fullscreen mode Exit fullscreen mode

👉 But internally they are NOT the same thing

🔬 Internal Reason
For declared variable:

let a;
typeof a // "undefined"
Enter fullscreen mode Exit fullscreen mode

👉 Engine:

Find a → value is undefined → return "undefined"
Enter fullscreen mode Exit fullscreen mode

For undeclared variable:
typeof b

👉 Engine does something special:

Check if variable exists
IF NOT → return "undefined" instead of throwing error
Enter fullscreen mode Exit fullscreen mode

🛡️ 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");
}
Enter fullscreen mode Exit fullscreen mode

👉 Safe even if someVar is never declared

❌ Without typeof

if (someVar !== undefined) {
  // ❌ ReferenceError
}
Enter fullscreen mode Exit fullscreen mode

🔬 Internal Behavior (Spec-Level Concept)
Normally variable access:

ResolveBinding(name)

If not found:

→ ReferenceError
Enter fullscreen mode Exit fullscreen mode

But typeof does:

If binding not found → return "undefined"
Enter fullscreen mode Exit fullscreen mode

👉 Special handling in spec

Real-World Use Case

Feature detection

if (typeof window !== "undefined") {
  // browser environment
}
Enter fullscreen mode Exit fullscreen mode

👉 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)
Enter fullscreen mode Exit fullscreen mode

🎯 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)