ReferenceError: The code refers to a variable or function that does not exist (has not been declared). A ReferenceError in JavaScript means the code used a variable or function name that does not exist in the current scope. A typo, a missing import or using a variable before it is declared with let or const are common causes.
The message names the missing identifier, such as ReferenceError: userName is not defined, which usually makes the fix quick.
What should you keep in mind with ReferenceError?
These errors often appear only in some situations, like a code path that runs rarely or a browser without a feature you assumed. TypeScript and linters such as ESLint catch most of them while you type, before the code ever runs. A bundler that reports missing imports at build time helps too. Treat a ReferenceError in production as a sign that a check is missing from your build process.
Master error handling patterns across languages including exceptions, Result types, error propagation, and graceful degradation to build resilient applications. Use when implementing error handling, designing APIs, or improving application reliability.