As a die-hard java-developer for several years I was quite annoyed by the fact that Java as a language lagged several years behind C#. (E.g. Java got lambdas last year, 6 years after C#, so standards are nice for compatibility, but tends to be slow to evolve.)
Polyfill
Now, as a frontend-developer and getting to know JavaScript, I have realized that JavaScript is lagging even further behind. Take for example the everyday case of checking if a string contains another string:
This is awkward and you know it, however JavaScript is evolving. The next version ECMAScript 6 is in its final phase, and will have, among several features, the following method:
Another new nice feature is for finding the first item in an array. It used to be written like this:
And now it can be written like:
ECMAScript 6 is feature complete, and is started to be supported by modern browser like Chrome, Firefox and Internet Explorer 11. As for older browsers, great polyfills like core-js come to our rescue.
Transpile
While polyfills extend current objects with new methods, ECMAScript 6 also contains new language syntax. Let’s look at the arrow function (also known as fat arrow):
Older browser will not understand “=>” (for a long time!), so this code has to be transpiled to ECMAScript 5:
There are some great transcompilation tools out there, and the one we have used is Babel. You can even play around with it here.
Scoped variables
JavaScript will also get scoped variables (let and const):
This will transpile to:
Classes
And finally, JavaScript is getting support for classes (which is sugar around prototype), and the syntax looks like this:
This will be transpiled to the following, not very readable, but working code:
Conclusion
ECMAScript 6 is here, and we should start using it. Older browser will be around for a long time, so I think we should get used to transpiling code on the web-platform.
Tor Arne Senior developer