Paul Krill
Editor at Large

CoffeeScript 2 arrives with JavaScript syntax improvements

news
Sep 19, 20172 mins

Upgrades should require 'little or no refactoring' for most projects, but there are some breaking changes

intel coffee lake figurative image java
Credit: Amenic181 / Thinkstock

CoffeeScript, a simple languages that compiles to JavaScript and aims to make web developers’ lives easier, has just moved to a second major release, one that emphasizes syntax improvements. 

CoffeeScript 2, which had been in a beta stage since April, features a compiler that translates CoffeeScript code into modern JavaScript syntax. A CoffeeScript “class” is now output using the class keyword, for example. Version 2 also features support for async functions syntax, the future object destructuring syntax, and JSX, which is JavaScript with interspersed XML elements.

The primary goals of CoffeeScript 2 were to remove incompatibilities with JavaScript that might prevent CoffeScript from being used for a project, and to preserve backward compatibility to the greatest degree possible.

New capabilities were done with few breaking changes from the 1.x version, CoffeeScript developers said. Previously, the team had warned that CoffeeScript 2 would require breaking changes to conform with the ECMAScript 2015 specification and above. “Most current CoffeeScript projects should be able to upgrade with little or no refactoring necessary,” the developers said. The few breaking changes involve bound (fat arrow) functions, bound generator functions, and Literate CoffeeScript parsing, which has been refactored to be more careful about not treating indented lists as code blocks.

Modernizing of JavaScript syntax means developers may need to transpile the compiler’s output, with transpilation converting source code into equivalent but different source code. CoffeeScript documentation cites the case in which developers would want to convert modern JavaScript into older JavaScript to run in older versions of Node.js or older browsers. Built-in support for the Babel transpiler is included in CoffeeScript.

With version 2, CoffeeScript now outputs line comments, after having discarded them in version 1.1. Block comments are now permitted anywhere, enabling static type annotations. The coffee command-line tool has been improved in the upgrade as well.

Note that some JavaScript features have been intentionally omitted, such as let and var, named functions, and the get and set keywords. Whereas let and named functions were omitted for simplicity’s sake, var was left out so that CoffeeScript developers would not have to deal with variable declarations, and the get and set keywords were omitted to avoid grammatical ambiguity. CoffeeScripts omission of these JavaScript features does not affect compatibility or interoperability with JavaScript modules or libraries. 

Paul Krill

Paul Krill is editor at large at InfoWorld. Paul has been covering computer technology as a news and feature reporter for more than 35 years, including 30 years at InfoWorld. He has specialized in coverage of software development tools and technologies since the 1990s, and he continues to lead InfoWorld’s news coverage of software development platforms including Java and .NET and programming languages including JavaScript, TypeScript, PHP, Python, Ruby, Rust, and Go. Long trusted as a reporter who prioritizes accuracy, integrity, and the best interests of readers, Paul is sought out by technology companies and industry organizations who want to reach InfoWorld’s audience of software developers and other information technology professionals. Paul has won a “Best Technology News Coverage” award from IDG.

More from this author