Puneet Varma (Editor)

CoffeeScript

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
Designed by
  
Jeremy Ashkenas

OS
  
Cross-platform

Paradigm
  
Multi-paradigm: prototype-based, functional, imperative, scripting

Developer
  
Jeremy Ashkenas, Colin Runciman, et al.

First appeared
  
December 13, 2009; 7 years ago (2009-12-13)

Stable release
  
1.12.2 / December 16, 2016; 2 months ago (2016-12-16)

CoffeeScript is a programming language that transcompiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python and Haskell in an effort to enhance JavaScript's brevity and readability. Specific additional features include list comprehension and pattern matching.

Contents

CoffeeScript support is included in Ruby on Rails version 3.1 and Play Framework. In 2011, Brendan Eich referenced CoffeeScript as an influence on his thoughts about the future of JavaScript.

History

On December 13, 2009, Jeremy Ashkenas made the first Git commit of CoffeeScript with the comment: "initial commit of the mystery language." The compiler was written in Ruby. On December 24, he made the first tagged and documented release, 0.1.0. On February 21, 2010, he committed version 0.5, which replaced the Ruby compiler with a self-hosting version in pure CoffeeScript. By that time the project had attracted several other contributors on GitHub, and was receiving over 300 page hits per day.

On December 24, 2010, Ashkenas announced the release of stable 1.0.0 to Hacker News, the site where the project was announced for the first time.

Syntax

Almost everything is an expression in CoffeeScript, for example if, switch and for expressions (which have no return value in JavaScript) return a value. As in Perl, these control statements also have postfix versions; for example, if can also be written after the conditional statement.

Many unnecessary parentheses and braces can be omitted; for example, blocks of code can be denoted by indentation instead of braces, function calls are implicit, and object literals are often detected automatically.

Interval test

To compute the body mass index, one may do (here in JavaScript):

With CoffeeScript the interval is directly described:

Loops and comprehensions

To compute the greatest common divisor of two integers with the euclidean algorithm, in JavaScript one usually needs a while loop:

Whereas in CoffeeScript one can use until and pattern-matching instead:

Any for loop can be replaced by a list comprehension; so that to compute the squares of the positive odd numbers smaller than ten (i.e. numbers which remainder modulo 2 is 1), one can do:

Alternatively, there is:

You can implement a linear search with a one-liner using the when keyword:

The for ... in syntax allows you to loop over arrays while the for ... of syntax allows you to loop over objects.

You can use the ? keyword to quickly check if a variable is null or undefined :

This would alert "No person" if the variable is null or undefined and "Have person" if there is something there.

Functions and jQuery

A common JavaScript snippet using the jQuery library is:

Or even just:

In CoffeeScript, the function keyword is replaced by the -> symbol, and indentation is used instead of curly braces, as in other off-side rule languages such as Python and Haskell. Also, parentheses can usually be omitted, using indentation level instead to denote a function or block. Thus, the CoffeeScript equivalent of the snippet above is:

Or just:

String interpolation

Ruby-style string interpolation is included in CoffeeScript. Double-quoted strings allow for interpolated values, using #{ ... }, and single-quoted strings are literal.

Compiling

The CoffeeScript compiler has been written in CoffeeScript since version 0.5 and is available as a Node.js utility; however, the core compiler does not rely on Node.js and can be run in any JavaScript environment. One alternative to the Node.js utility is the Coffee Maven Plugin, a plugin for the popular Apache Maven build system. The plugin uses the Rhino JavaScript engine written in Java.

The official site at CoffeeScript.org has a "Try CoffeeScript" button in the menu bar; clicking it opens a modal window in which users can enter CoffeeScript, see the JavaScript output, and run it directly in the browser. The js2coffee site provides bi-directional translation.

Latest additions

  • Source maps allow users to de-bug their CoffeeScript code directly, supporting CoffeeScript tracebacks on run time errors.
  • CoffeeScript supports a form of Literate Programming, using the .coffee.md or .litcoffee file extension. This allows CoffeeScript source code to be written in Markdown. The compiler will treat any indented blocks (Markdown's way of indicating source code) as code, and ignore the rest as comments.
  • Adoption

    On September 13, 2012, Dropbox announced that their browser-side code base has been rewritten from JavaScript to CoffeeScript.

    GitHub's internal style guide once said "write new JS in CoffeeScript", and while it no longer does, all the advice in the style guide references how to write good CoffeeScript, and their Atom text editor is also written in the language.

    References

    CoffeeScript Wikipedia