undefined vs not defined in JavaScript
undefined is a value. not defined is an error. Here is the real difference and why confusing them causes bugs.
undefined vs not defined in JavaScript
undefined and "not defined" look similar but are completely different. One is a value, the other is an error.
undefined Is a Value
undefined is a special value in JavaScript. It means a variable has been declared but has not been assigned a value yet.
let x; console.log(x); // undefined console.log(typeof x); // "undefined"
During the memory allocation phase, var variables are initialized to undefined. let and const are placed in the TDZ, but after the declaration line without an assignment, they are also undefined.
"not defined" Is an Error
"not defined" is what the engine says when a variable does not exist in any accessible scope. It throws a ReferenceError.
console.log(y); // ReferenceError: y is not defined
The variable y was never declared anywhere, so it is "not defined."
The Confusing Error Message
console.log(z); // ReferenceError: z is not defined
The error message says "z is not defined," which sounds like undefined. But it means z does not exist at all. The wording is a known source of confusion.
typeof and Safety
typeof on an undeclared variable returns "undefined" without throwing:
console.log(typeof w); // "undefined" (no error)
This is the safe way to check for a variable that may not exist (e.g., checking if a global API is available).
Common Sources of undefined
- A declared but unassigned variable.
- A function parameter that was not passed.
- A function with no
returnstatement. - Accessing a property that does not exist on an object.
- The result of
void 0.
Common Sources of "not defined"
- Using a variable that was never declared.
- Misspelling a variable name.
- Using a variable outside its scope (e.g., a
letoutside its block). - TDZ access for
let/const(the message is slightly different: "Cannot access 'x' before initialization").
The Takeaway
undefined is a value meaning "declared but no value yet." "not defined" is a ReferenceError meaning "does not exist in any scope." typeof returns "undefined" for both but does not throw for undeclared variables, making it safe for existence checks.
undefined is a value assigned to a declared variable that has no value yet. not defined is a ReferenceError thrown when a variable does not exist in any accessible scope.
Because z was never declared, so it does not exist in any scope. The error message 'not defined' means the variable is missing entirely, which is different from undefined which means the variable exists but has no value.
Use typeof. typeof an undeclared variable returns 'undefined' without throwing a ReferenceError. This is the safe way to check for globals or optional APIs.
Declared but unassigned variables, missing function parameters, functions with no return, accessing a non-existent object property, and the result of void 0.
undefined is a property of the global object, not a reserved keyword. In modern JavaScript it is non-writable, so you cannot reassign it, but in old engines you could. Avoid relying on it being a keyword.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

