Caffeine fix

Freshly brewed CoffeeScript 2 joins the modern JavaScript era

Jane Elizabeth
CoffeeScript
© Shutterstock / iravgustin

It’s been nearly two years since we last heard from CoffeeScript. The latest news is definitely worth the wait: CoffeeScript 2 is finally available. This latest release has all sorts of goodies and extras, bringing CoffeeScript into the modern JavaScript era.

When we last heard from CoffeeScript, they had just released 1.9 with great acclaim. That was in 2015. But in the meantime, they’ve taken advantage of all that extra time for an extra shot of CoffeeScript 2.

CoffeeScript 2.0

This new release brings CoffeeScript into the modern JavaScript era. It’s been intended to close compatibility gaps with JavaScript while maintaining its hallmark clean syntax.

CoffeeScript 2 has two main goals: to remove any incompatibilities with JavaScript and to preserve as much backward compatibility as possible.

CoffeeScript claims that there are very few breaking changes from 1.x. Most of your current CoffeeScript projects should be able to upgrade without any issues. (However, there may be some refactoring necessary. Them’s the breaks.)

SEE MORE: Get rid of runtime errors in the cloud with JavaScript Studio

Modern JavaScript Output

One of CoffeeScript’s goals from the very beginning was to be just like JavScript. This latest release brings CoffeeScript even closer to JavaScript ES6.

Many of the latest JavaScript features such as => are nearly one-to-one compatible in this latest release. CoffeeScript’s innovative features are straightforward for outputting.

Here are some of the following CoffeeScript features that were updated in 2.0 to follow modern JavaScript syntax.

  • Modules: import/export
  • Classes: class Animal
  • Async functions: await someFunction()
  • Bound/arrow functions: =>
  • Function default parameters: (options = {}) ->
  • Function splat/rest parameters: (items...) ->
  • Destructuring, for both arrays and objects: [first, second] = items{length} = items
  • Object rest/spread properties: {options..., force: yes}{force, otherOptions...} = options
  • Interpolated strings/template literals (JS backticked strings): "Hello, #{user}!"
  • Tagged template literals: html"<strong>coffee</strong>"
  • JavaScript’s for…of is now available as CoffeeScript’s for…from (we already had a for…of): for n from generatorFunction()

However, not all CoffeeScript features will seamlessly work in JavaScript. For example, default values in JavaScript are only applied whem a variable is undefined, not undefined or null as before. Also, classes have their own differences as well. (Gotta check those breaking changes!

SEE MORE: The ultimate JavaScript plugin cheat sheet

Some more fun goodies:

  • CoffeeScript 2 includes a compiler that translates CoffeeScript code into a modern JavaScript syntax.
  • Support for async functionssyntax
  • Support for the future object destructuring syntax
  • Line comments are now output (in CoffeeScript 1 they were discarded)
  • Block comments are now allowed anywhere, enabling static type annotations using Flow’s comment-based syntax
  • Support for JSX

So, are you interested in the latest version of CoffeeScript? If so, head on over to their site and get 2.0!

Author
Jane Elizabeth
Jane Elizabeth is an assistant editor for JAXenter.com

Comments
comments powered by Disqus