15

In the last couple of months, I've been learning a lot about JavaScript. Having abused the languages for years, I dare say that I now have a better understanding of the language and I've come to love the benefits of its functional nature.
Lately I've taken up learning Scheme, but that's just for fun. Browsing the MDN reference I noticed that JS, though lacking block scope, does have a keyword that can be used to declare a variable local to a given block, much like Scheme's let:

for (var i=0;i<someArray.length;i++)
{
    console.log(someArray[i]);
}
console.log(i);//will log someArray's length

Whereas:

for (let i=0;i<someArray.length;i++)
{
    console.log(someArray[i]);
}
console.log(i);//undefined

So what I want to know now is: Why isn't let used more often? Has it got something to do with X-browser support? Is it just one of those lesser-known-goodies?
In short, what are the advantages of using var over let, what are the caveats?

As far as I can tell, the behaviour of let is, if anything, more consistent (double declarations in a single block raise a TypeError, except for function bodies (ECMA6 drafts fix this, though).
To be honest, apart from this feature/keyword not being very well known, I struggle to think of any argument against using let for loops, or anywhere where a temporary variable makes for more readable code.

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149

1 Answers1

28

Yes, it has entirely to do with browser support. Currently only Firefox implements it (since it's part of their superset of ECMAScript).

But it is coming in ES6, so there's hope...

I doubt it could be said that there are many benefits to var over let given that both are supported. I think the mantra is going to be "let is the new var"

I Hate Lazy
  • 47,415
  • 13
  • 86
  • 77
  • Can't accept your answer (5 more minutes), but you're absolutely right, and I share your hopes... Silly, though, I'm currently using Chrome, and if I had just taken the time to open my console and try `let x = 1;` I'd have known there was a problem there. Silly, but still: `let`'s hope everybody who reads this question puts in a request to support let ASAP in the ECMA mailing list :) – Elias Van Ootegem Nov 26 '12 at 15:59
  • 1
    @EliasVanOotegem: Yeah, last I heard their target is December 2013, so we've got some waiting, but you can enable some of the new features that have been implemented in Chrome by setting a startup flag to allow experimental JavaScript. Not sure if `let` is included among those. To set those, type `chrome://flags/` in the address bar of chrome. – I Hate Lazy Nov 26 '12 at 16:11
  • Just enabled experimental features, and `let` is supported, however there are some differences between FF and chrome, though part of it might be down to the way the console _eval_'s the code. Anyway a big +1 and thanks for the link – Elias Van Ootegem Nov 27 '12 at 08:10
  • TypeScript will have `let` support in the 1.4 version: https://github.com/Microsoft/TypeScript/pull/904 – Vadorequest Nov 19 '14 at 10:30
  • @Vadorequest: That's besides the point, given that TS is just transcompiled into JS. This question is about the various JS implementations, not transcompilers/X-compilers and how they deal with ES6 features. – Elias Van Ootegem Nov 29 '14 at 08:37