16

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.

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
  • @Kaiido So, it's a bug right? Because, Chrome has the expected error message for this case. –  Jul 08 '16 at 04:07
  • 4
    If you think it is a bug, than make a bug report and they will say yay or nay. – epascarello Jul 08 '16 at 04:10
  • @Bergi The error message confuses me because I thought it would say something is wrong with the variable 'y'. But instead, it says something is wrong with the variable that's on the first line in my example. –  Jul 08 '16 at 04:12
  • 1
    the bug exists in release version 47.0.1 as well – Jaromanda X Jul 08 '16 at 04:16
  • 1
    [Pushlog](https://hg.mozilla.org/integration/mozilla-inbound/pushloghtml?fromchange=d6059530b0317e6f6b141582b611469505256be4&tochange=cfc1820361f599c55128b29de4332f8d06511e07). Regressed by [bug 589199](https://bugzilla.mozilla.org/show_bug.cgi?id=589199) or [1202902](https://bugzilla.mozilla.org/show_bug.cgi?id=1202902). – Oriol Jul 08 '16 at 04:25
  • 3
    The minimal code would be `var x; { y; let y; }` – Oriol Jul 08 '16 at 04:32
  • Have you reported it yet? – Oriol Jul 08 '16 at 16:37
  • @Oriol Nope! Not yet. More research is needed to know if this issue is big enough to be posted on bugzilla :) –  Jul 08 '16 at 17:58
  • @Devashish It's not a very important issue, but it definitely shouldn't behave like this. You can always inform of problems, the worst that can happen if they are not important enough is that nobody will fix them. – Oriol Jul 08 '16 at 18:03
  • @Oriol okay, give me a day. I need to learn how to post bugs on the forum. It is going to be my first post. –  Jul 08 '16 at 18:08
  • 4
    @Oriol I reported the bug. Here is the link: https://bugzilla.mozilla.org/show_bug.cgi?id=1285710 –  Jul 09 '16 at 04:49
  • I see the bug 1285710 is marked as fixed in Firefox 53 on their bug tracker – Martin Smith Mar 17 '17 at 05:25
  • 6
    Yes I tested this in version _54.0a2_ and see `ReferenceError: can't access lexical declaration 'y' before initialization`. I am voting to close this because it can't be reproduced anymore. – Sᴀᴍ Onᴇᴌᴀ Mar 17 '17 at 05:32

1 Answers1

1

This post is made in accordance with this meta post, instead of being closed (see this).


As @Martin Smith and @Sam Onela said (1, 2), this issue was resolved in version 53/54. Relevant bugzilla report, made by Devashish (original poster).

Community
  • 1
  • 1
LarsW
  • 1,514
  • 1
  • 13
  • 25