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

Mark
  • 53
  • 5
  • 1
    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. – Pointy Jul 13 '21 at 01:31
  • @Pointy 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? https://i.imgur.com/7vprT1Q.png – matthew-e-brown Jul 13 '21 at 02:09
  • 1
    @matthew-e-brown 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. – Pointy Jul 13 '21 at 02:25
  • @Pointy True. Personally, I think it's quite a nifty feature. ^-^ – matthew-e-brown Jul 13 '21 at 03:37
  • @matthew-e-brown 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. – Pointy Jul 13 '21 at 12:42

1 Answers1

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.

matthew-e-brown
  • 2,837
  • 1
  • 10
  • 29
  • 1
    and I guess redeclaration of `const` would not make sense anyway – Pointy Jul 13 '21 at 02:26
  • @matthew-e-brown I prefer chrome console following MDN definition. In my opinion, refreshing the page is a minor thing, but violating MDN spec is intolerable. – Mark Jul 13 '21 at 20:18
  • 1
    @Mark 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](https://262.ecma-international.org/6.0/), 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?* – matthew-e-brown Jul 13 '21 at 22:18