let statement in loop doesn't work as expected in IE


let statement in loop doesn't work as expected in IE



I am trying some example in ECMAScript 6. That is working differently compared to other browsers. This returns true in Firefox, but it returns false in IE. Why is this working different in Internet Explorer?


true


false


let callbacks =
for (let i = 0; i <= 2; i++) {
callbacks[i] = function () { console.log(i); return i * 2 }
}
console.log(callbacks[0]() === 0);
console.log(callbacks[1]() === 2);
console.log(callbacks[2]() === 4);





IE doesn't support ES6 fully.
– Rajaprabhu Aravindasamy
Aug 26 '16 at 6:30





ok.I does not show any errors. But it returns the value wrongly.
– Raavanan
Aug 26 '16 at 6:31





wrong because IE doesn't support ecma6 very well
– Jaromanda X
Aug 26 '16 at 6:34





It's a known problem with IE that let within the for(...) initialisation doesn't work as it should. The simplest workaround is to add let j = i; as the first line inside the { } block and then use j within your function - that gives the result you expect.
– nnnnnn
Aug 26 '16 at 6:34



let


for(...)


let j = i;


{ }


j





Actually it's not just IE. No single javascript engine, either in the browser or server currently support the complete ES6 spec. If you want to use ES6 features either use only the very limited subset that has wide browser support or use a transpiler like babel. If you want to use pure js, use ES5. ES6 cannot be used as is in production. It's OK for hobby projects because you only need it to run in your own browser so you can use the small subset your browser supports. But it's not OK for real production code.
– slebetman
Aug 26 '16 at 8:51




1 Answer
1



According to caniuse.com IE11 kind of supports let:



let variables are not bound separately to each iteration of for loops






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV