We're looking for a person or people to help audit MDN to find places we could speed up. Is this you or someone you know? Check out the RFP: https://mzl.la/2IHcMiE

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

Nossos voluntários ainda não traduziram este artigo para o Português (do Brasil). Junte-se a nós e ajude-nos a fazer o trabalho!
Você também pode ler o artigo em English (US).


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

Etiquetas do documento e colaboradores

 Colaboradores desta página: fscholz, johnjago, jonathanKingston
 Última atualização por: fscholz,