5

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!

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
nick_rinaldi
  • 641
  • 6
  • 17
  • 1
    Why would you imagine that the same variable name could be made to hold more than one value? – Pointy Oct 28 '20 at 22:58
  • 1
    I can't reproduce this. – Aplet123 Oct 28 '20 at 22:58
  • 1
    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? – Phil Oct 28 '20 at 22:58
  • What console? Browser consoles and Node REPL are not quite the same as running continuous code "normally." – Guy Incognito Oct 28 '20 at 22:59
  • 1
    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. – Guy Incognito Oct 28 '20 at 23:00
  • @Phil, the error only shows when pasting **both** lines together, when placing separate, no errors are shown. – 0stone0 Oct 28 '20 at 23:00
  • In Safari console even running them consecutively throws a duplicate error. – pilchard Oct 28 '20 at 23:01
  • @0stone0 that's a fair callout. Firefox behaves as expected too (throws an error). Edge behaves like Chrome – Phil Oct 28 '20 at 23:03
  • Hey all. I was running it in chrome. Indeed the error appears upon pasting those two lines, but apparently chrome allows let re-declarations. – nick_rinaldi Oct 28 '20 at 23:44

1 Answers1

9

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.

Phil
  • 157,677
  • 23
  • 242
  • 245
  • 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! – nick_rinaldi Oct 28 '20 at 23:43
  • 1
    @nick_rinaldi no problems. It's a shame you copped a bunch of downvotes initially. I don't think people fully understood what you were asking unfortunately – Phil Oct 29 '20 at 00:37
  • It's expected. I tried my best to explain it as thoroughly as possible, but I figured I could always do a better job. – nick_rinaldi Oct 29 '20 at 00:57