-7

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 global window object like those defined with var.

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 the for() loop and var 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?

16
  • 2
    If you really want rep, link to Documentation examples for these topics ;) Commented Nov 29, 2016 at 15:46
  • 3
    I've never seen the term "temporal dead zone" in the ECMAScript language specification. Variables defined using let or const declarations are defined when their lexical environment is initialized, but not accessible until the variable's lexical binding has been evaluated.
    – user4639281
    Commented Nov 29, 2016 at 16:37
  • @TinyGiant But that's how this is commonly called. Commented Nov 29, 2016 at 16:43
  • 2
    Regardless, that is a colloquialism of sorts, and not including it should not count against the answer as that is a matter of personal preference. My suggestion would be to edit the answer to correct any actual technical inaccuracies. If the author reverts your edit, then your next step would be to post an answer to the existing question.
    – user4639281
    Commented Nov 29, 2016 at 16:46
  • @TinyGiant Posting an answer when a question already has many upvoted answers just doesn't work. I think we already went through this. Commented Nov 29, 2016 at 16:47
  • For sure, but you say that many of the answers spread misinformation and are technically incorrect. Such answers require moderation. You could always post on meta asking for help with that, or hop into a chat room and do the same. Once a question no longer has too many answers, that no longer applies. If all of the answers were factually correct and did not repeat information already existing on the page, then you might have an issue.
    – user4639281
    Commented Nov 29, 2016 at 16:50
  • I have tried the same thing as this in the past (though I left the original alone and attempted to point people to my example instead), and it just doesn't work. The only option is to work with the existing question in some way.
    – user4639281
    Commented Nov 29, 2016 at 16:54
  • @TinyGiant What would asking on Meta or in chat achieve? Upvoted answers can't be deleted by 20k rep users, and moderator usually don't delete answers because they're incorrect (one of the standard reasons for rejecting a flag is "flags should not be used to indicate technical inaccuracies, or an altogether wrong answer"). Commented Nov 29, 2016 at 17:02
  • Are you not familiar with the meta effect? </tongue-in-cheek>
    – user4639281
    Commented Nov 29, 2016 at 17:07
  • Also, @gothdo: "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." if you removed the word window from the sentence it would be technically correct for all implementations that follow the ECMAScript language specification correctly.
    – user4639281
    Commented Nov 29, 2016 at 17:22
  • @TinyGiant Yes, and so what? Commented Nov 29, 2016 at 17:24
  • 1
    A single word edit hardly seems worth having a point about (not trying to say that invalidates your question, I just don't think it is worth mentioning as it is such an easy fix), just edit the word out.
    – user4639281
    Commented Nov 29, 2016 at 17:24
  • @TinyGiant But that's not the only thing which is wrong with that answer. Commented Nov 29, 2016 at 17:25
  • 3
    The problem here, it seems, is that you would like to post a better answer to the question, but you think that because there are already too many existing upvoted answers, it will be pointless. That is what needs to be addressed. How can we have a better method of posting a new answer to an old upvoted question with many answers, and get it noticed in a meaningful way? Bounties may be an option, but I'm not so sure that would help. Posting on meta asking may be an option, but it could also look like you're just looking for rep. Posting in chat asking for upvotes is pretty much outlawed.
    – user4639281
    Commented Nov 29, 2016 at 17:39
  • 2
    Visible is the correct terminology (available isn't a concept in JS, but would mean something different), but "temporal dead zone" is definitely not. So far as I'm concerned, as a JS dev, the answer is better without your proposed edits. The difference is scoping, anything else is an artifact of when (and how) scopes are created.
    – ssube
    Commented Nov 29, 2016 at 20:20

1 Answer 1

6

I think the best solution is to create a new canonical Q&A and close the old one as a duplicate of it.

No, that won't work out. The existing question is too popular and well-linked.

Instead, post your own answer as a new answer to the canonical question (since you already posted it elsewhere, I'd suggest a moderator to merge it into there). It does have very good and clear examples, and will certainly score a few dozen upvotes on such a high-trafficed question. It's certainly a better answer than many of the others.

Of course, it'll hardly ever score better than the currently accepted answer - but that's not really necessary either. The top answer should be simple and short, targeted at the main audience for the questions: newbies who wonder about let vs var. The main difference is scoping, that's all what is relevant for them. We should fix the minor inaccuracies you pointed out, but not expand on all the possible details.

Other answers focus on browser compatibility etc, yours can focus on the technical side (TDZ, global object, redeclarations, hoisting, behaviour in loops etc). It will make the answers more diverse and the canonical even better.

2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .