1

Why can let declare variables repeatedly in the browser console?

I remember that I would have reported an error before. Is 21-year chrome based on that ECMA-specification?

let a=1
undefined

let a=12
undefined

navigator.userAgent
'Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko) Chro
me/93.0.4577.82 Safari/537.36'

Here's a screenshot:

enter image description here

showdev
  • 28,454
  • 37
  • 55
  • 73
andrew yin
  • 31
  • 5
  • Also related: [Why reassigning const producing error in console only if we do that in code editor?](https://stackoverflow.com/q/69054932/5648954) – Nick Parsons Sep 23 '21 at 01:53

2 Answers2

4

The console environment has a number of odd quirks. It's not like a standard <script> tag on the page.

One of the quirks is these. The way it used to be in Chrome was, re-declarations of variables with let was not permitted - you'd get the expected error that the re-declaration is invalid syntax. But as of Chrome 80 or so (spring 2020), it was changed to be permitted.

You still can't re-declare variables with let in a standard <script>, of course:

let foo = 5;
let foo = 10;
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • 2
    Key bit from the linked post: "The inability to redeclare using let and class was a common annoyance amongst web developers who experiment new JavaScript code in the Console." – ray Sep 23 '21 at 01:52
  • Thank you very much for your answer. This is exactly where I am confused. The processing results of console and script are different. Very curious where did you know this? My English is not good, there may be grammatical errors, sorry – andrew yin Sep 23 '21 at 03:53
  • @andrewyin I knew this because I noticed the change when it happened more than a year ago, since I do a lot of typing in the console, and also sometimes check the Chrome release notes. – CertainPerformance Sep 23 '21 at 03:54
  • You must be a programming master. I hope that one day I can also become an excellent programmer. Do you have any recommended learning materials about javascript? – andrew yin Sep 23 '21 at 03:58
0

You cannot redeclare a variable previously declared with let in strict mode. Please see: What's the difference between using "let" and "var"?.

George Sun
  • 968
  • 8
  • 21