Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scoping problems when using let in Firefox Scratchpad

The following code produces an error when I run it in Firefox 52 Scratchpad:

function scope() {
    let x = 1;
}

let x = 2;

/*
Exception: SyntaxError: redeclaration of let x
@Scratchpad/8:1:1
*/

How to explain that? The first x should be encapsulated in the function and not interfere with the second declaration.

Running this code as a snippet in Chrome, or inside an HTML page with a <script> tag in Firefox doesn't trigger the exception. Also wrapping it in a function, or even a pair of {} brackets eliminates the problem.

Could it be a bug in Scratchpad?

like image 693
Krzysztof Szafranek Avatar asked Oct 17 '22 15:10

Krzysztof Szafranek


1 Answers

It works if you only run it one time. The second time you try the same code, the original let x = 2; is still alive.

like image 191
Ken H. Avatar answered Oct 21 '22 08:10

Ken H.