Search code examples
javascriptecmascript-6variable-declarationgoogle-chrome-console

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


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!


Solution

  • 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.