16

A question for my elders in JavaScript: putting aside the fact that let has a different variable scope than var does, it seems to me that in the vast majority of cases, there is no reason to use var over let. Of course, if you define a variable inside of an if statement, it's not the same thing, but is there another reason besides scope to use one over the other?

Edit: To be clear, this question is about a best practice (i.e. pros and cons) in JavaScript, not about the nature of the differences between var and let.

Thanks!

informatik01
  • 16,038
  • 10
  • 74
  • 104
daveslab
  • 10,000
  • 21
  • 60
  • 86
  • 2
    @jedi, no, this one is different – Max Koretskyi Oct 15 '16 at 11:48
  • 2
    Duplicate finders overreactive as always :D – Robo Robok Oct 15 '16 at 11:49
  • http://stackoverflow.com/questions/762011/let-keyword-vs-var-keyword-in-javascript – abhirathore2006 Oct 15 '16 at 11:51
  • Actually, this was a duplicate. There are differences, do the math. It's no longer a duplicate, because the answer mentions a substantial difference. `let` doesn't leak to `window`. P.S. 'Best practice' questions are 'opinion-based' per se. – Estus Flask Oct 15 '16 at 11:54
  • @estus, yes, I mention that in my answer. But with the proper written modular code, no `var` statement should be allowed to leak to `windows` and so this has no importance. And the question is not about differences, it's about whether one should prefer `var` over `let` in ES6 code – Max Koretskyi Oct 15 '16 at 11:57
  • @Maximus Since the question is vague on the environment and doesn't presume that the code is executed inside IIFE or a module, I consider this an important clarification. – Estus Flask Oct 15 '16 at 12:00
  • @estus, anyways, I mentioned that in my answer :) – Max Koretskyi Oct 15 '16 at 12:02
  • 4
    @estus I don't agree that "best practice" questions are opinion-based. What is sick about Stack Overflow is that all those questions abusers are usually taking questions too directly. "Best practice" means "pros and cons of", but they read it as "what is your opinion on". Many, many great questions have been abused this way here. – Robo Robok Oct 15 '16 at 12:08
  • 1
    @daveslab, The only reason I can think of not switching everything to let is temporal dead zone. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#Temporal_dead_zone_and_errors_with_let – Yasin Yaqoobi Oct 15 '16 at 12:09
  • 2
    @RoboRobok, I agree that stackoverflow has many problems, including this one that annoys me as well. Besides, I've recently started answering questions and the biggest demotivator is that answer about simple stuff get 3-5 likes, while when I spent an hour going through OP's code or explain in great details a concept get only one like from the OP at best – Max Koretskyi Oct 15 '16 at 12:13
  • @Maximus True story, my brother. That is screwed up too. – Robo Robok Oct 15 '16 at 12:17
  • @YasinYaqoobi, why would you need to declare a variable twice? – Max Koretskyi Oct 15 '16 at 12:18
  • @RoboRobok, and also too many unattended questions. OP asks them and then never gets back for clarification and never accepts an answer. However, I like asking hard questions here and get answers :). – Max Koretskyi Oct 15 '16 at 12:20
  • @Maximus it is not just about declaring the variable twice. There are other edge cases, if you scroll down but i agree, most of these are just bad practice to begin with. – Yasin Yaqoobi Oct 15 '16 at 12:21
  • @RoboRobok Listing pros and cons for each option is one thing. Labeling one of the compared options as 'best' is another. The problem is there can be no pros and cons for this subject, only naked facts. – Estus Flask Oct 15 '16 at 13:04

1 Answers1

19

No, I think there are no reasons. From what I've read, the var is left in ES6 for backwards compatibility. Most of the articles I've read advise to gradually replace all var occurrences with let. Of course, this should be done accounting for differences such as the one you mentioned with if block. I use only let in my ES6 code now. Eric Elliott doesn't use var either. There’s nothing using var that let can’t do better. If you need entire function scoping, create the let at the top of the function.

There is one difference that I've noticed. It is that when defining variables in the global scope, variables defined as var are accessible with window.variable, while variables with let are not accessible using window object. But with the properly written modular code, no var statement should be allowed to leak to window and so this has no importance.

I would also advise to use const instead of let for the cases when a variable reference (as opposed to a primitive variable's value) will not be mutated.

Trunk
  • 742
  • 9
  • 24
Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488