for...of - JavaScript keyword Internet Explorer work-around

310 views Asked by At

I am using “for...of” in many places in my Javascript code which is a new technology, part of Javascript’s new specification (ECMAScript 2015 (E56) standard). "for...of" is supported in Chrome, Safari and Firefox. Though, IE doesn’t yet support this new feature (See this link). What would be my best approach to support these 4 main browsers - do I revert to "for in/for loop" or do I use browser-sniffing code or is there a better hack to be able to both use some hybrid "for...of" and "for...in"? This is not a repeat of this question here: link_here because I am aware that IE does not support "for...of". What I am looking for is if there is a hack to still use it and still support all the 4 browsers I listed?

2

There are 2 answers

0
Oriol On BEST ANSWER

You can add the Traceur compiler:

<script src="https://google.github.io/traceur-compiler/bin/traceur.js"></script>
<script src="https://google.github.io/traceur-compiler/src/bootstrap.js"></script>
<script type="module">
for(var n of ['foo', 'bar'])
  alert(n);
</script>

It should work even on IE9.

0
balrob On

If you want back compat support for "for...of" your only choice is a compiler (transpiler). If you look here: https://kangax.github.io/compat-table/es6/ the well known compilers, and the level of support they provide, are well documented.