This is a follow-up to my question User refusing to correct misinformation in their popular answer. Please take a look at it before reading this question. I'm posting this question because after 5 days, the problem hasn't been solved yet, and my question is still locked.
There's a popular JavaScript question What's the difference between using “let” and “var” to declare a variable?.
What's wrong with the top answer?
The top answer is wrong about many points. It starts with:
The difference is scoping.
Calling it "the difference" implies that it's the only difference, but there are other differences, for example the temporal dead zone. It's explained later that there indeed are other differences, but it's still confusing.
Also, variables declared with let are not accessible before they are declared in their enclosing block. As seen in the demo, this will throw a ReferenceError exception.
This should mention that the reason of this is the temporal dead zone.
Demo:
This demo is a huge block of code. I don't think it's very readable. It would be better if it was split into smaller pieces, each with separate explanation what's happening there.
However, global variables defined with
let
will not be added as properties on the globalwindow
object like those defined withvar
.
window
is not a part of ECMAScript. It's available only in browsers, and this question is not only about JavaScript code that runs in a browser.
They are identical when used like this in a function block.
They aren't, because as the answer previously said, "variables declared with let
are not accessible before they are declared". Saying that "they have identical scope" would be correct.
let
is only visible in thefor()
loop andvar
is visible to the whole function.
This might be nitpicking, but IMO "visible" is not a good word to describe that. "available" would be much better.
Assuming strict mode,
var
will let you re-declare the same variable in the same scope. On the other hand,let
will not
This works the same outside strict mode.
Also, the only example of block scoping that this answer shows is a loop, but you can have a block without a loop. This answer also doesn't mention using functions inside a loop with let
, which behaves differently than with var
(see JavaScript closure inside loops – simple practical example).
"Just edit it!"
I could, but why would I do that? Call me a rep whore (I'm aware that the term "rep-whore" has been retired, but apparently it's OK to use it when talking about yourself), but I don't want to spend a lot of time and put a lot of effort into doing something that won't give me any reputation. Quoting another Meta question (the context is slightly different, but the main point is that we deserve reputation for the good content we create):
- Why shouldn't I get rep on a Q&A I worked on for a week?
- Why shouldn't I get the badges?
- Why would I actually take the time to make a canonical answer if I know it's not going to be accepted well, I'm going to get much headaches and eventually, no reward from it?
Yeah, we get rep for our excellent question and answer. DEAL WITH IT.
Also, it's much easier to just write a new answer, instead of editing that one to be correct.
Moreover, editing it wouldn't solve all problems. There are other upvoted answers on that question, and some of them include misinformation (for example, this answer claims that variables declared with let
are not hoisted, but in fact they are).
There's a problem with the question itself too. It asks only about let
, but all differences between var
and let
also apply to const
. However, we can't close questions about const
as a duplicate of that, because it's only about let
.
The solution
I think the best solution is to create a new canonical Q&A and close the old one as a duplicate of it. I already posted it, but it's currently locked because of "content dispute".
Side note: one user pointed out one thing that needs to be changed in my answer. I will edit it as soon as it gets unlocked.
Is this a good solution? If yes, can a moderator unlock my Q&A? If not, what would be a better solution?