Why can let declare variables repeatedly?

115 Views Asked by At

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

2

There are 2 best solutions below

1
On

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

4
On

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;