I'm often asked why I still use CoffeeScript rather than ES6/7. Well, in truth I use both, but I do still prefer CoffeeScript for the following reasons:
- Correctly trimmed multiline strings (ES6 includes the indentation because it doesn't have significant whitespace)
- Existential operator (
a?.b
;b?()
;a?.b?()
and of coursea ? b
)a ? b
is more appropriate thana || b
if""
or0
are values ofa
you wish to persist(a && a.b && typeof a.b === 'function') ? a.b() : undefined
doesn't quite have the same ring to it asa?.b?()
- Significant whitespace
- (a personal preference, but since you already lay your code out sensibly why do you need the additional visual distraction of braces?)
- Convenient shorthand for common things (
->
(regular non-bound function),@
(this.
),::
(.prototype.
)) in
to check if something in a lista in b
is a lot clearer and harder to mess up thanb.indexOf(a) >= 0
- Chained comparisons
a < b < c
(minor) - Ranges
[a..b]
e.g. in loopsfor a in [1..10]
- Shorthand slicing
a[b..c]
/a[..]
(minor) - Block regexps (a very good way to allow documenting of complex regexps)
- Safe switch statements (never forget a
break
again; minor since this can be linted against)
For more you might be interested in my Long Live CoffeeScript and Long Live ES6 post.
There's a lot to like about ES6/7 though; here's some things that I miss from Babel when writing CoffeeScript:
let
/const
; and to be honestvar
(CoffeeScript should support an initial assignment operator such asa := b
)- The ternary operator
a ? b : c
(CoffeeScript doesn't have this because of the existential operator and implicit object syntax; however I think that could be solved by switching toa ?? b
and giving the ternary operator higher precedence than implicit objects?) - spreading objects
{...a, b: 'c'}
(CoffeeScript has spread for arrays, but not for objects; easy to work around withObject.assign({}, a, b: 'c')
but I like sugar)