Undefined vs Not Defined

Published: (February 24, 2026 at 10:30 AM EST)
3 min read
Source: Dev.to

Source: Dev.to

Undefined

undefined is a special keyword in JavaScript. It means the variable exists in memory, but no value has been assigned yet.

During the creation phase of an execution context JavaScript:

  1. Sees a declaration such as var x.
  2. Allocates memory for x.
  3. Assigns a default value → undefined.

Because the memory slot for x already exists, accessing x before an explicit assignment logs undefined instead of throwing an error.

undefined is not “empty”; it occupies its own memory space and acts as a placeholder until the variable receives a concrete value.


Not Defined

A “not defined” situation occurs when a variable has never been allocated memory.

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

The error does not mean “value missing”; it means JavaScript has no knowledge of the identifier y at all.

During the creation phase, JavaScript scans for declarations (var, function, let, const). Since y was never declared, no memory slot is created, and any attempt to access it results in a ReferenceError.

  • undefined → memory exists, value does not.
  • not defined → memory does not exist.

This determination happens before execution starts, not at runtime.


Creation vs. Execution Phases

JavaScript processes code in two distinct phases:

  1. Memory Allocation (Creation Phase)

    • Declarations are hoisted.
    • Memory slots are created and, for var, initialized to undefined.
  2. Execution Phase

    • Code runs line‑by‑line, using the memory slots prepared earlier.

If a variable was allocated (e.g., var x), the execution phase can read its value (undefined until assigned).
If a variable was never allocated (e.g., y), the execution phase throws a ReferenceError.


let and const

let and const also receive memory during the creation phase, but JavaScript blocks access to them until they are initialized. This is known as the Temporal Dead Zone (TDZ). Attempting to read them before their declaration results in a ReferenceError, even though a memory slot exists.

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

Conclusion

undefined and “not defined” are not just minor wording differences; they reflect fundamentally different memory states in JavaScript:

  • undefined – a declared variable exists in memory but lacks an assigned value.
  • Not defined – the identifier was never declared, so no memory slot exists.

Understanding this distinction clarifies how JavaScript prepares code before it runs and helps avoid common runtime errors.

0 views
Back to Blog

Related posts

Read more »