Javascript let double declaration
Asked Answered
M

1

3

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

enter image description here

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

enter image description here

Mercedes answered 13/7, 2021 at 1:31 Comment(5)
Chrome's console wraps each console line of code in { } 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
@Rehm Do you have some source for that? I don't think it's true... if every line was wrapped in { } 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.pngLachance
@Lachance yes I agree, and can offer no explanation, but clearly the behavior is weird, and there are other quirky things about the tools. The bottom line is that all you can do really is live with the behavior.Rehm
@Rehm True. Personally, I think it's quite a nifty feature. ^-^Lachance
@Lachance yes and I realized after reading the answer below that const and let should be the same as far as scope rules go, but the const semantics about the declared variable being a constant are probably worth maintaining.Rehm
L
5

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

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 const binding.

Lachance answered 13/7, 2021 at 2:0 Comment(3)
and I guess redeclaration of const would not make sense anywayRehm
@Lachance I prefer chrome console following MDN definition. In my opinion, refreshing the page is a minor thing, but violating MDN spec is intolerable.Mercedes
@Mercedes MDN isn't a spec nor definition; it's hosted by Mozilla, who are independent of ECMA International. Also, if you take a look at the official JS spec, the word 'console' does not appear even once, and it doesn't specify how to handle the resultant SyntaxError. Personally, I take this to mean that browsers can do whatever they want with their console. This behaviour only occurs when you're declaring variables directly in the console. Can you give me a reason why you'd ever want a declaration to fail due to a redeclaration in the console?Lachance

© 2022 - 2024 — McMap. All rights reserved.