Krzysztof Szafranek
Krzysztof Szafranek

Reputation: 788

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?

Upvotes: 5

Views: 761

Answers (1)

Ken H.
Ken H.

Reputation: 408

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.

Upvotes: 3

Related Questions