45

EDIT: Please read the question! I already know the difference. This is not a duplicate.

Obviously, right now I should always be using the var key word as let isn't supported in everything.

When the let keyword has better support (say, I'm writing a Node application in a couple years time which uses Harmony), when should I use the let keyword vs the var keyword?

I understand the difference —var is for function scoping while let is for block scoping—but I'm looking for something like "always use the let keyword" or "use the var keyword at the top of functions, but the let keyword in blocks like for loops".

sebagomez
  • 9,029
  • 6
  • 48
  • 84
callumacrae
  • 7,341
  • 7
  • 28
  • 45
  • As the implementations are still in progress we don't really know how it will be regarding to performances. – Denys Séguret Feb 20 '14 at 11:19
  • [Javascript - “let” keyword vs “var” keyword](http://stackoverflow.com/questions/762011/javascript-let-keyword-vs-var-keyword) – Deepak Ingole Feb 20 '14 at 11:19
  • 4
    Well, already 2 people rushing to answer without bothering to read the question... – Denys Séguret Feb 20 '14 at 11:19
  • 1
    And people voting for it to be closed as a duplicate. This hasn't been asked before—I searched for a while. – callumacrae Feb 20 '14 at 11:24
  • @CallumMacrae From the other question : *"When should let be used over var?"* – Denys Séguret Feb 20 '14 at 11:26
  • @dystroy Didn't see that. The only answers are answering the first half of the question, though. – callumacrae Feb 20 '14 at 11:28
  • You can use `let` **inside loops** to create a closure **without having to write an extra function layer** – Paul S. Feb 20 '14 at 11:32
  • `"use the var keyword at the top of functions, but the let keyword in blocks like for loops"`. I'm imagining you pretty much answered yourself here, no? Honestly, I can't think of a better explanation. If someone has one, please come forward. – victorantunes Feb 20 '14 at 11:38
  • 1
    @victorantunes here is an in-depth discussion of `var vs. let`, with the author concluding that var is a better default so that specific uses of `let` stand out more. http://davidwalsh.name/for-and-against-let – Jon Coombs May 05 '15 at 18:14
  • @callumacrae another thing to keep in mind is closures. Quoting from the article above, "the ES6 specification actually says that let i in a for loop header scopes i not only to the for loop, but to each iteration of the for loop." Here, `let` can help eliminate unexpected bugs in loops, but use `var` when you happen to want the old behavior. – Jon Coombs May 05 '15 at 18:15
  • consider reopen this question will allow further discussion on this topic. Each use cases between `var` and `let` will lead to different advantages. For example: `for`, `if` is surely more advantages using `let` because the scope simply defined in the block. And the default declaration will still goes to `var` – Yeo Jul 24 '15 at 07:01
  • This may be the best explanation of 'let'. Good examples also: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/let – Teo Nov 23 '16 at 08:52

3 Answers3

35

I would say that you should, as a principle, use let whenever it is not inconvenient to do so. Such as:

for (let i = 0; i < 100; i++) {
    // Do something
}

if (condition) {
    let msg = a + b + c;
    console.log(msg);
    alert(msg);
}

The advantages to this approach is:

  1. Less risk of overriding some global variable use for something else
  2. Less risk of memory leaks due to variables staying in memory long after they have become irrelevant
neelsg
  • 4,250
  • 5
  • 30
  • 55
  • 3
    Those considering using `let` as their default choice instead of `var` may want to give this a bit more thought first...there's a good article about the pros and cons here: http://davidwalsh.name/for-and-against-let. It's not quite as simple as "use `let` whenever it is not inconvenient to do so". That might still be some programmers' conclusion in the end but it would be good to understand the pros and cons first... – Matt Browne May 26 '15 at 20:53
  • 5
    Hi @MattBrowne. Thanks for the link, but I don't think the article makes a very good case. Basically what it says is: (1) if you use a variable before declaring using let, your code breaks, (2) if you use a variable outside the block it was created in, your code breaks, (3) if you find and replace "var" with "let" in poorly written existing code, your code breaks, (4) there is a cool proposal for let that didn't make it into the final standard, (5) if you use let everywhere, you don't communicate if you meant block scoping or function scoping... The only point here worthy to consider is (5) – neelsg May 27 '15 at 08:20
  • 7
    @neelsg And (5) is a very poor case, anyway. If you really want something visible *outside* of a nested scope (such as at the function level, rather than in an `if` statement), then you should declare it outside the nested scope. That communicates intent far more clearly, and is consistent with the vast majority of other languages. – Mud May 28 '15 at 18:58
  • I think what David Walsh writes [about let and var](https://davidwalsh.name/for-and-against-let) sounds reasonable and gives a lot of examples what you should do and what not. Also recommended is his article about [coercion, i.e. implicit casts in JavaScript](https://github.com/getify/You-Dont-Know-JS/blob/master/types%20&%20grammar/ch4.md#chapter-4-coercion). If you are not aware, they can lead to many bugs (when do you use `==` and when `===`?) ;-) ... – Matt Mar 28 '18 at 08:43
16

Use let as a general rule, and var on occasion.

Block scoping is the standard and most readable choice, and will make debugging easier. Block scoping makes it easy to see exactly where a variable is in scope. Function scoping makes things a lot less apparent, and much easier to accidentally introduce bugs with scoping mistakes.

In general, the smaller the scope you can use, the better. Thus let over var.

In particular, it helps deal with the endless problem of closing over variables and not realising their value will change before the closure is executed:

for (var i = 1; i <= 5; i++) {
  var item = document.createElement("LI");
  item.appendChild(document.createTextNode("Item " + i));

  let j = i;
  item.onclick = function (ev) {
    alert("Item " + j + " is clicked.");
  };
  list.appendChild(item);
}
Phil H
  • 18,593
  • 6
  • 62
  • 99
-6

Well the answer is quite simple use var if you need function scoping and let if you need block scoping.

ooxi
  • 2,553
  • 2
  • 22
  • 39