Why can 'let' be re-declared in the devtools console in Chrome? (Other browsers don’t allow it.)
Asked Answered
H

1

6

I'm learning about the alternate variable declarations introduced in ES6. Right now, I have learned that the 'let' variable declaration is block scoped, and although it can be updated, it cannot be re-declared in the same scope.

My first question is: Is the above information true? I am reading an article from April 2, 2020. Things may have changed.

My second question is: If the above information is true, then how come in my Chrome console, when I run

let greeting = 'hello';

and the following line I re-declare it such as

let greeting = 'say hi now';

the value is changed to 'say hi now' and no error is reported.

I'm thinking it has something to do with the semicolon at the end of the line, but not sure. Just a JS noobie here. Thanks!

Hu answered 28/10, 2020 at 22:56 Comment(9)
Why would you imagine that the same variable name could be made to hold more than one value?Barfield
I can't reproduce this.Satterlee
Running those two lines of code in my console (Chrome), I get "Uncaught SyntaxError: Identifier 'greeting' has already been declared". Running it in the Node REPL (13.7.0), I get "Uncaught SyntaxError: Identifier 'greeting' has already been declared". What console are you running it in?Ferrate
What console? Browser consoles and Node REPL are not quite the same as running continuous code "normally."Burget
FWIW it throws an error in Chrome console if you copy-paste both lines at the same time but not if you run the lines separately.Burget
@Phil, the error only shows when pasting both lines together, when placing separate, no errors are shown.Robb
In Safari console even running them consecutively throws a duplicate error.Icono
@Robb that's a fair callout. Firefox behaves as expected too (throws an error). Edge behaves like ChromeFerrate
Hey all. I was running it in chrome. Indeed the error appears upon pasting those two lines, but apparently chrome allows let re-declarations.Hu
F
10

This is a Google Chrome specific feature

Support for let and class redeclarations in the Console

The Console now supports redeclarations of let and class statements. The inability to redeclare was a common annoyance for web developers who use the Console to experiment with new JavaScript code.

In experimenting, I found that IE 11 and Edge behave the same as Chrome in that typing each command separately produces no error but executing both in a single evaluation does.

See also https://bugs.chromium.org/p/chromium/issues/detail?id=1004193


Other experiments show that Firefox and the NodeJS REPL do not offer such a feature.

Ferrate answered 28/10, 2020 at 23:8 Comment(3)
Yep I'm running it in chrome. This is the type of info I'd hope to receive in asking this question. Marked as accepted. Thanks!Hu
@Hu no problems. It's a shame you copped a bunch of downvotes initially. I don't think people fully understood what you were asking unfortunatelyFerrate
It's expected. I tried my best to explain it as thoroughly as possible, but I figured I could always do a better job.Hu

© 2022 - 2024 — McMap. All rights reserved.