Fetching latest headlines…
undefined vs undeclared, and how typeof behaves
NORTH AMERICA
πŸ‡ΊπŸ‡Έ United Statesβ€’April 17, 2026

undefined vs undeclared, and how typeof behaves

1 views0 likes0 comments
Originally published byDev.to

🧩 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".

Comments (0)

Sign in to join the discussion

Be the first to comment!