ReferenceError: can't access lexical declaration`X' before initialization

The JavaScript exception "can't access lexical declaration `variable' before initialization" occurs when a lexical variable was accessed before it was initialized. This happens within any block statement, when let or const declarations are accessed before they are defined.

Message

ReferenceError: Cannot access 'X' before initialization (Edge)
ReferenceError: can't access lexical declaration `X' before initialization (Firefox)
ReferenceError: 'x' is not defined (Chrome)

Error type

ReferenceError

What went wrong?

A lexical variable was accessed before it was initialized. This happens within any block statement, when let or const declarations are accessed before they are defined.

Examples

Invalid cases

In this case, the variable "foo" is accessed, even before it is declared which throws an reference error ( because variables declared using let/const are not hoisted ).

function test() {

  // Accessing the variable foo before it's declared 

  console.log(foo);     // ReferenceError: can't access lexical
  let foo = 33;         // 'foo' is declared here using the 'let' keyword
   
}
test();

Valid cases

In the following example, we correctly declare a variable using the let keyword before accessing it, so we encounter no error. And in contrast, notice how we are accessing the bar variable even before it is declared — without encountering any error. That's because of the hoisted nature of var variables.

function test(){

   // Declaring variable foo
   let foo = 33;
   console.log(foo, bar);    // 33 undefined
   var bar = 12;
}
test();

See also

© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cant_access_lexical_declaration_before_init