Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using "let" in javascript in terms of browser compatibility?

What are the options for using the let keyword in javascript. It seems like it would be really useful.

I've seen traceur, but I'm wondering if there are any other options, so I don't have to run entire projects through it.

Is it even possible with some type of polyfill or library. Or do I basically have to wait until all the old browsers die out to use it natively...

like image 274
gkiely Avatar asked Nov 27 '12 09:11

gkiely


People also ask

Is let supported in all browsers?

EDIT: let and const are supported by all modern browsers and are part of the ECMAScript 2015 (ES6) specification.

Can I use let in JavaScript?

let allows you to declare variables that are limited to the scope of a block statement, or expression on which it is used, unlike the var keyword, which declares a variable globally, or locally to an entire function regardless of block scope.

Does Internet Explorer support let?

Internet Explorer browser version 6 to Internet Explorer browser version 10 doesn't supports. Internet Explorer browser version 11 partially supports JAVASCRIPT let and partial support for Internet Explorer refers ot supporting an older version of the web browser.


2 Answers

Let me quote what I just read on You Don't Know JS: Scope & Closures - Appendix B: Polyfilling Block Scope

PS: This is licensed under Creative Commons CC BY-NC-ND 3.0 so it is OK to share it as long as the reference is mentioned

Large quote ahead:

Consider the following example

{
 let a = 2;
 console.log( a ); // 2
}

console.log( a ); // ReferenceError

This will work great in ES6 environments. But can we do so pre-ES6? catch is the answer.

try{throw 2}catch(a){
 console.log( a ); // 2
}
console.log( a ); // ReferenceError

Whoa! That’s some ugly, weird looking code. We see a try/catch that appears to forcibly throw an error, but the “error” it throws is just a value 2, and then the variable declaration that receives it is in the catch(a) clause. Mind: blown.

That's right, the catch clause has block-scoping to it, which means it can be used as a polyfill for block scope in pre-ES6 environments.

"But...", you say. "...no one wants to write ugly code like that!" That's true. No one writes (some of) the code output by the CoffeeScript compiler, either. That's not the point.

The point is that tools can transpile ES6 code to work in pre-ES6 environments. You can write code using block-scoping, and benefit from such functionality, and let a build-step tool take care of producing code that will actually work when deployed.

This is actually the preferred migration path for all (ahem, most) of ES6: to use a code transpiler to take ES6 code and produce ES5-compatible code during the transition from pre-ES6 to ES6.

like image 126
Ahmad Alfy Avatar answered Nov 12 '22 06:11

Ahmad Alfy


There is absolutely no way to mimic / shim a keyword in ECMAscript. So you either choose to totally go with it (which might be a very bad idea at present) or you can't use it yet.

As you might know, let is part of ECMAscript Next / Harmony and at best, its just available as experimental feature in some browsers. Even if let is a very stable feature, ES.Next spec is still in flux even. Go play with it, but it really makes no sense to rely on it imho.

like image 41
jAndy Avatar answered Nov 12 '22 05:11

jAndy