I know let in JS cannot be declared the variable twice. But when I try the below code in my console:
a = 1;
let a = 2;
let a = 3;
.
.
.
let a = 100;
Note: They are run line by line (as shown in the screenshot below). The version is Google Chrome 91.0.4472.114

It always works, no error. This really confuses me, why it works fine? I know what happens in the console isn't indicative of what happens in a script. But my question is why this exists in console? Is there any reason for that, or it might be a bug?
Because I suppose that let and const have the same declaration behavior, if I use const instead of let, there is no doubt about it.
b = 1;
const b = 2;
const b = 3; //Uncaught SyntaxError: Identifier 'b' has already been declared

This is an explicit feature of Chrome, and nothing more. It's handling things for you, to make it so that you don't run into the roadblock of 'x is already defined' when you're fiddling around testing.
It was added in Chrome 80.
The Console now supports redeclarations of
letandclassstatements. The inability to redeclare was a common annoyance for web developers who use the Console to experiment with new JavaScript code.
Edit:
I have just found that Chrome 92, coming soon, will add support for const redeclarations, too!
This allows developers to copy-paste code into the DevTools console to see how it works or experiment, make small changes to the code, and repeat the process without refreshing the page. Previously, DevTools threw a syntax error if the code redeclared a
constbinding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With