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:
type let name = 'Jack';
press Enter
type let name = 'John';
No Error given, name returns 'John'
It is not working if:
type let name = 'Jack';
type let name = 'John';
press Enter
Error, Uncaught SyntaxError: Identifier 'name' has already been declared
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.
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!â
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 &
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.
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â ?