Declare with let in Chrome bug

Hi,

I think I have found a bug in Chrome.
let does not let you re-declare variable however you can do in Chrome.

If you open the developer tools then in console you can re-declare the same variable so I assume you can have bug in an app or website using Chrome. However I think tests will pick it up and works normal in Firefox.

How to create the issue:

  1. type let name = 'Jack';
  2. press Enter
  3. type let name = 'John';
  4. No Error given, name returns 'John'

It is not working if:

  1. type let name = 'Jack';
  2. type let name = 'John';
  3. press Enter
  4. Error, Uncaught SyntaxError: Identifier 'name' has already been declared

Bug reported to Google

1 Like

You can’t redeclare let variables in the same scope in a JavaScript program, this is the designed behaviour, it won’t work anywhere. Let allows you reassign to the same variable name, it fixes the var behaviour where you can redeclare a variable with the same name in the same scope, which is almost never something wanted. It should always throw that error: if it doesn’t in the FF console, then that’s a convenience so that you don’t have to refresh the window when you’re trying things out.

let name = "Jack"
name = "Jill"
1 Like

@DanCouper,

This is what I am saying you cannot redeclare using let. However you can do in Chrome Version 80.0.3987.122 (Official Build) (64-bit)

How to create the issue:

  1. type let name = 'Jack';
  2. press Enter
  3. type let name = 'John';
  4. No Error given, name returns 'John'

It should not happen however you can redeclare in Chrome this way.
I have redeclared the name property

if you think it is a bug, you can direct your query to google chrome support
here we can at most say “oh, yeah, you are right, what a weird thing!”

1 Like

@DanCouper, @ilenia,

I have got answer from Google and this is the expexted behaviour.

2 Likes

Ah, sorry, the way you’d written the OP made it sound like it did that in FF rather then in Chrome – yeah

It’s really annoying having to refresh when you’re testing different combinations of things out & you’re using class/let declarations (I normally don’t include let/const keywords in an attempt to avoid this issue, and then cause problems when I paste code back into my editor), this change is sensible & :+1:

I am sorry if I was not clear.
Yes, this change is definitely good.

1 Like

No, was fine! I just didn’t read it properly :grimacing:.

It often surprises me when speaking to other JavaScript developers how many are unaware of how variable scope works in the language they code in. By scope we are talking about the visibility of variables in our code.

Yeah, was just as surprised as you to see that I could now declare a let variable twice, without an error. :grimacing:
From reading your link, I see that it’s only in the console, when typing a let line after let line. Const still throws an error.

However, nothing has changed with let in ES6. const and let are still block scoped and you can only declare them once in a block scope. “If the let or const can see each others declaration” , a SyntaxError results:

“Uncaught SytaxError: Identifier ‘yourvariablename’ has already been declared”

To see , use the IIFE example below.

(function () { let x = 2; let x = 3;})();
// javascript ES6 has not changed, just chrome console.
// no change for const in the console.

Ha! Thanks Chrome! You scared me! Imagine I was trying to explain this to someone and could not show them how let could only be declared once in a block scope!

Begs the question, how’s someone learning going to grasp the concept and experiment with block scope and the functionality of let. Does this further confuse js coders?

It’s a trade-off: slightly unxepected behaviour in a debugging environment (the environment is important here – it’s specifically there to debug code) in exchange for making life easier. It’s really annoying having to reload the page every time you need to try something (this occurs in particular when you paste code in that you’re testing)

True and agree. I’ve also revised my opinion. I did find it quite irritating having to choose another variable name. Might have been helpful to be able to turn it off with something like, “Use Strict” ?

1 Like