Quick Overview
`undefined` means the name exists holding nothing; not defined means it was never declared and reading it throws. `typeof x` is the only check that survives the second case without crashing.
Beginners often confuse undefined with not defined. While they sound identical in everyday English, they represent two completely different engine states in JavaScript!
Understanding this distinction makes diagnosing runtime ReferenceError crashes instant and painless.
undefined: Memory HAS been allocated for the variable by the JS Engine during Phase 1 (Memory Creation), but no real value has been assigned yet. It is a legitimate primitive value and type in JavaScript.not defined: No memory was ever allocated for the variable name in the current scope. Trying to read a non-existent variable throws an uncaught ReferenceError!// Declared variable without an explicit initial value
let currentUser;
console.log(currentUser); // undefined (Memory allocated, no value assigned)
// Trying to read a variable that was never declared
console.log(totalScore); // Uncaught ReferenceError: totalScore is not defined
Phase 1 (Memory Creation) -> Variable 'currentUser' allocated -> Set to undefined
Phase 2 (Code Execution) -> Reading 'currentUser' -> Output: undefined
-> Reading 'totalScore' -> ReferenceError: not defined!
user = undefined: Avoid manually writing user = undefined to clear a variable. Use null instead to signal an intentional empty state, leaving undefined exclusively for uninitialized engine defaults.if (x === undefined) can throw a ReferenceError if x is undeclared. Use typeof x === "undefined" for safe checks on potentially undeclared globals!Write it yourself. The tests below run against your code.
Remember: undefined means declared without a value, while not defined means undeclared in memory. Next, let's master how functions encapsulate reusable logic!
Primitives copy by value and objects copy by reference, so `b = a` duplicates a number but shares an object. That single distinction explains most surprise mutations in a codebase.
`const` prevents reassignment, not mutation: a `const` object's properties can still be changed. `let` is scoped to its block, while `var` is function-scoped and leaks out of loops.
Only backticks interpolate: `${user}` reads the variable, while the identical text in quotes prints literally. Strings are immutable, so every method returns a new one rather than editing in place.