Before jumping into the new syntax wins, it is important to note, at time this article was written, ES6 is not supported by all browsers. To take advantage of the newest features, the Babel plugin will help to transpile ES6 to today's ES5. Check out the Babel documentation for details on that.
CoffeeScript prides itself with the ability to leave off variable assignments, which makes this first feature hard to compare directly to CoffeeScript, but knowing the difference between
var can be helpful when converting CoffeeScript to ES6.
let keyword actually works the same as
var except that it is bound by the lexical scope, which means you can only access the
let within the bounds of the closure's it was written in.
vars however can be hoisted in the global scope unintentionally.
lets are the new
*example inspired by this Stack Overflow post
let's scope within the function prevents potential memory leaks and unintended variable assignments.
=> is very similar to the
=> in CoffeeScript and with the addition of some curly braces will work similarly.
One key difference with
=> is that
this has lexical scope, which means the surrounding context of the function is inferred on the inside of the
=> function(more details on that here).
export replaces the old
modules.export and comes with the key word
default for function imports without destructuring (see below).
Destructuring is another similarity with CoffeeScript. ES6 does give you the ability to destructure modules. I use destructuring all the time now when pulling out methods from libraries.
Template strings are great if you are used to string interpolation in CoffeeScript.
Luckily the ECMAScript 6 team had this in mind.
That's the basics, but to find out more general tips on ES6, check out ponyfoo's article on the subject here.
It's apparent that ES6 is not too far off from what you get with CoffeeScript and actually requires the addition of variable assignments,
If you are looking to implement the latest features in the language, ES6 is definitely the direction you want to migrate towards.