ReferenceError: Use before delaration (Edge) ReferenceError: can't access lexical declaration `X' before initialization (Firefox) ReferenceError: 'x' is not defined (Chrome)
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.
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 } } test();
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); } } test();
© 2005–2018 Mozilla Developer Network and individual 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