Meaning: A variable is declared but has no value assigned.
Happens when:
- You declare a variable but don't initialize it.
- A function parameter is missing in a call.
- You explicitly assign undefined.
Example:
var a;
console.log(a); // undefined
function greet(name) {
console.log(name);
}
greet(); // undefinedKey Points:
- undefined is a special value in JavaScript.
- Declared variables are hoisted and initialized to undefined (in the creation phase).
- It's a valid value but usually means "no value assigned yet".
Meaning: The variable has never been declared in the current scope.
Example:
console.log(b); // ReferenceError: b is not definedKey Points:
- Accessing an undeclared variable causes a ReferenceError.
- This happens because the variable doesn't exist in memory at all.
| Feature | undefined | not defined |
|---|---|---|
| Declaration | ✅ Declared | ❌ Never declared |
| Memory allocation | ✅ Yes (value = undefined) | ❌ No |
| Error on access | ❌ No error | ✅ ReferenceError |
| Example | var a; console.log(a); // undefined |
console.log(b); // ReferenceError |
"undefined means a variable has been declared but not assigned a value, so it's a value and type in JavaScript. not defined means the variable hasn't been declared at all, so accessing it throws a ReferenceError. Variables declared with var are hoisted and initialized to undefined, while accessing undeclared variables immediately causes an error."