Is it safe to use the new syntactic sugar for Javascript callbacks?

Posted on

Question :

These days I was testing some Javascript features in Google Chrome 50 and I noticed that it has already been added to arrow function for callbacks.


$.each([1, 2, 3], x => x * 2);

// [2, 4, 6]

Formerly it would have to do like this:

$.each([1, 2, 3], function (x) { return x * 2; })

Can we safely use this option for all browsers (we’re in 2016!), or do I still have to wait a little bit longer?


Answer :

It’s not safe. ECMAScript 6 is not receiving 100% feature support even in browsers (even the modern ones), and even if the latest browser version supports these features you will limit the way your site works for those browsers. The ideal thing is to program with features of ECMAScript 5, I am an adept at this idea, although there are people who use other techniques for the code to run with ECMAScript 3. Everything depends on your audience, generally the support for ECMAScript 5 is essential .

You can follow the browser support and much more here:

And if you still want to use ECMAScript 6, look for other alternatives like Babel, Traceur, Webpack …

In this personal project of a friend I recently did a branch where I rejuvenated all the code for ES6. I use Webpack with Babel in this project and it works in old browsers because the code is transpiled to ES5.



Leave a Reply

Your email address will not be published. Required fields are marked *