Accessing a variable that has not been declared in the current scope.
A ReferenceError is thrown when you try to access a variable that has not been declared. Unlike accessing a property on an object (which returns `undefined`), referencing an undeclared variable is a hard error in JavaScript.
Common causes: a typo in the variable name, using a variable declared with `let` or `const` before its declaration (Temporal Dead Zone), using a block-scoped variable outside its block, or forgetting to declare a variable with `let`, `const`, or `var`.
1function calculateTotal(price, quantity) {2 const discount = 0.1;3 const subtotal = price * quantity;4 // 'diccount' is a typo — should be 'discount'5 const total = subtotal - subtotal * diccount;6 return total;7}8 9console.log(calculateTotal(100, 5));1function calculateTotal(price, quantity) {2 const discount = 0.1;3 const subtotal = price * quantity;4 // Fix: Use the correctly spelled variable name5 const total = subtotal - subtotal * discount;6 return total;7}8 9console.log(calculateTotal(100, 5)); // 45010 11// Example: Temporal Dead Zone fix12// BAD:13// console.log(myVar); // ReferenceError14// const myVar = 5;15 16// GOOD:17const myVar = 5;18console.log(myVar); // 5Simulate standard system builds to trigger compiler trace records and track memory crashes locally.
Inside `calculateTotal`, the code references `diccount` — a misspelling of `discount`. JavaScript looks for `diccount` in the current scope chain and doesn't find it. `let` and `const` variables also cause ReferenceErrors when accessed before their declaration (Temporal Dead Zone).