Take a look at the below code:
var c = 50;
{
console.log(y);
let y = 50;
}
This code is expected to throw a ReferenceError and it does. But in the console, the message attached with the exception just blown my mind. It says:
ReferenceError: can't access lexical declaration 'c' before initialization
In the code c
is the first variable declared. The error message indicates that something is wrong with the declaration of c
. It's clearly the y
variable inside the block, causing the exception. We can't use variables declared using let
before its declaration. When I declare another variable in the first line, say test
, the error message changes to:
ReferenceError: can't access lexical declaration 'test' before initialization
Am I missing something or am I right about the bug? I have the latest Firefox Developer Edition (version 49.0a2).
Another thing worth noticing is that the block is simply an enclosing block, it is not the body of a function.
var x; { y; let y; }
– MagnificentReferenceError: can't access lexical declaration 'y' before initialization
. I am voting to close this because it can't be reproduced anymore. – Bobseine