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

Our volunteers haven't translated this article into Ελληνικά yet. Join us and help get the job done!
You can also read the article in English (US).


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

Error type


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.


Invalid cases

In this case, the variable "foo" is redeclared in the block statement using let.

function test() {
  let foo = 33;
  if (true) {
    let foo = (foo + 55); 
    // ReferenceError: can't access lexical 
    // declaration `foo' before initialization

Valid cases

To change "foo" inside the if statement, you need to remove the let that causes the redeclaration.

function test(){
   let foo = 33;
   if (true) {
      foo = (foo + 55);

See also

Εθελοντές και ετικέτες εγγράφου

Συνεισφέροντες σε αυτή τη σελίδα: mdnwebdocs-bot, PatrickKettner, fscholz, johnjago, jonathanKingston
Τελευταία ενημέρωση από: mdnwebdocs-bot,