2

Well, at ES6 we have a definition of block scope with let statement. It's good and we can use it at modern browsers.

What about old ones? How to support it? For example IE8, IE9. Is there any standard trick to achieve this?

Viacheslav Kondratiuk
  • 8,493
  • 9
  • 49
  • 81

3 Answers3

2

You can see the current compatibility here: https://kangax.github.io/compat-table/es6/#let

Notice that as no browser currently support it, you can use polyfills or compilers like https://google.github.io/traceur-compiler

From most cases, using var would be fine. Else you will have to encaspulate in some closure to simulate it.

Cyrbil
  • 6,341
  • 1
  • 24
  • 40
2

Use something like babel.js

Babel is a JavaScript compiler. Use next generation JavaScript, today.

Babel will 'transpile' your ES6 to ES5 - you may have to limit yourself to a subset of ES6 to support IE < 9, but from what I can tell, let is OK.

Dal Hundal
  • 3,234
  • 16
  • 21
2

Block-level scopes in ES6 heavily modifies the way the interpreters defines scopes, and evaluates code. There is no way to polyfill this behaviour on older browser.

The only solution for using ES6 features is to use an intermediate language such as Typescript which is EcmaScript compliant (you can use ES6 feature in it, including let and const).

You will then need to compile the Typescript code into Javascript code when building your application.

See also : What's new in Typescript 1.6

Halim Qarroum
  • 13,985
  • 4
  • 46
  • 71