JavaScript Error

ReferenceError

ReferenceError is thrown when code references a variable that doesn't exist in the current scope. This happens when using a variable before it's declared, or when the variable name is misspelled.

Common causes

  • Using a variable before it is declared (with let/const)
  • Typo in a variable or function name
  • Accessing a variable from the wrong scope
  • Forgetting to import a module or library
  • Using a browser API in a non-browser environment (e.g., window in Node.js)

How to fix it

  • Declare variables before using them
  • Check for typos in variable and function names
  • Use typeof to safely check if a variable exists: typeof x !== "undefined"
  • Ensure imports are present for external modules
  • Use try/catch when accessing environment-specific globals

Example

ReferenceError example
// Error example
console.log(myVariable);  // ReferenceError: myVariable is not defined

// Temporal dead zone
console.log(x);  // ReferenceError: Cannot access 'x' before initialization
let x = 10;

// Fix by declaring first
let myVariable = "hello";
console.log(myVariable);  // "hello"

// Safe check for existence
if (typeof myVariable !== "undefined") {
  console.log(myVariable);
}

Track ReferenceError with Checkend

Checkend automatically captures ReferenceError errors in your JavaScript application with full context:

  • Complete stack trace with source maps
  • Browser and environment context
  • Automatic grouping of similar errors
  • Instant notifications when errors occur

Stop debugging in production

Get full error context and fix issues faster with self-hosted error tracking.