2

This code:

var x = 8,
    y = 12;

let ( x = 5, y = 10) {
   return x + y;
} 

..gives "SyntaxError: Illegal let declaration outside extended mode"

But with "use strict" it works fine.

So it's interesting why 'let' first integrated with "use strict" mode ? (according to http://kangax.github.io/compat-table/es6/#nodeharmony)

kangax
  • 38,898
  • 13
  • 99
  • 135
ButuzGOL
  • 1,233
  • 2
  • 13
  • 27
  • 1
    What are you using to transpile? And/or what node version? – loganfsmyth Dec 28 '14 at 17:48
  • Works for me in FF (nightly) – kangax Jan 01 '15 at 14:19
  • @loganfsmyth node v0.11.14 – ButuzGOL Jan 05 '15 at 16:43
  • 1
    As far as I know, the reason is because people wanted to have a clear separation between older code and newer code, and the easiest way to do that would be to have "use strict" mark newer code. This is called [`Extended mode`](http://stackoverflow.com/questions/17253509/what-is-extended-mode). That said, I'm not actually sure if this is part of the final spec or just the current implementations. – loganfsmyth Jan 05 '15 at 17:04
  • Note however that the form of `let` your are using, the [`let expression`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#let_expressions) is a non-standard style supported only in Firefox. – loganfsmyth Jan 05 '15 at 17:05

1 Answers1

6

let is not a reserved word in ES3-era JavaScript. For instance, you could do this:

var let = 5;

which would declare a variable called let. The ES5 spec was forward looking, and made keywords they expected to use in the future reserved inside strict mode functions. So in the ES6 world, they can parse let as a keyword, but only inside strict containers. Outside strict containers, backwards compatibility demands that let be treated as an identifier, not a keyword.

Sean McMillan
  • 10,058
  • 6
  • 55
  • 65