ReferenceError: x is not defined
Locate the failed assumption, reproduce it, then repair the cause.
What actually caused it
The current chain has no accessible declaration for x. Check spelling, capitalization, imports, and whether the declaration lives inside a block or that this code cannot access.
A related but distinct failure happens when you read let or const before initialization. Browsers usually report “Cannot access 'x' before initialization” for that . That is not an undeclared ; the binding exists but is not ready to read.
A missing runtime can produce the undeclared-name error too. For example, document is supplied by a browser and is not available in an ordinary Node.js process.
Minimal reproduction
An undeclared binding
console.log(x);Run this in a that has no x declaration.
The related temporal dead zone
console.log(x);
const x = 5;This produces a different ReferenceError message: Cannot access x before initialization.
The fix
Declare the value before using it, in a shared by the work that needs it. Pass values into or return results instead of making every name global.
If the name should come from another , check its export and import. If the error is the variant, move the use after initialization and check for an inner declaration that accidentally shadows an outer one.
Declare before use
const x = 5;
console.log(x);Return a local result
function readX() {
const x = 5;
return x;
}
console.log(readX());