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
{ }
internally. edit yea you're right, it's weird. Note that there's no standard for behavior of a browser console. Each browser development team has its own ideas. Personally I develop on Firefox for the very reason that I find Chrome's tools really weird. – Rehm{ }
internally, you wouldn't be able to use declared variables at all, since their scopes would fall off immediately, would they not? i.imgur.com/7vprT1Q.png – Lachanceconst
andlet
should be the same as far as scope rules go, but theconst
semantics about the declared variable being a constant are probably worth maintaining. – Rehm