Hiprup

What is the difference between undefined and not defined?

They sound similar but mean different things.

  • undefined — the variable exists but has no value assigned.

  • not defined — the variable was never declared; accessing it throws a ReferenceError.

Safe check: typeof on an undeclared variable returns "undefined" without throwing.

let x;
console.log(x);         // undefined (declared, no value)
console.log(typeof x);  // 'undefined'

// console.log(y);      // ReferenceError: y is not defined
console.log(typeof y);  // 'undefined' (typeof is safe!)

// function parameters
function greet(name) {
  console.log(name); // undefined if not passed
}
greet(); // undefined (parameter exists but no argument)

// Object property
const obj = { a: 1 };
console.log(obj.b); // undefined (property does not exist)
// console.log(z);   // ReferenceError (variable does not exist)

x is declared (exists) but has no value → undefined. y is never declared → ReferenceError. typeof is the only safe way to check undeclared variables (does not throw). Function parameters without arguments and missing object properties are also undefined.

undefined = declared, no value (not an error). Not defined = not declared (ReferenceError). typeof is safe for both (returns 'undefined' for undeclared without throwing).

Missing function arguments and object properties are undefined, not 'not defined.'