Skip to main content

Command Palette

Search for a command to run...

what is undefined vs defined

Published
2 min readView as Markdown

1. Undefined

  • A variable is declared but not assigned any value.

  • JavaScript automatically assigns the value undefined to such variables.

  • It means: “The variable exists, but there is no value yet.”

Example:

var a;
console.log(a); // undefined

Here, a is declared, so it exists in memory, but it has no value assigned → undefined.


2. Not Defined

  • A variable is not declared at all in the scope.

  • Trying to access it will cause a ReferenceError.

  • It means: “The variable does not exist in memory.”

Example:

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

Here, b was never declared.


3. Key Difference

FeatureUndefinedNot Defined
Declaration statusDeclared but not assignedNever declared
ValueundefinedNo value (throws error)
Causes error?NoYes → ReferenceError

4. Example with Both

var x;
console.log(x); // undefined (declared but no value)

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

5. Relation to Hoisting

  • Variables declared with var are hoisted as undefined (not error).

  • Variables declared with let/const are hoisted but remain in Temporal Dead Zone (TDZ), so accessing before declaration → ReferenceError (similar to "not defined").

Example:

console.log(a); // undefined (because of var hoisting)
var a = 10;

console.log(b); // ReferenceError: Cannot access 'b' before initialization
let b = 20;

More from this blog

Amit singh's blog

235 posts