CoffeeScript Introduction and Resources

CoffeeScript (CS) is a concise programming language designed by Jeremy Ashkenas that compiles into standard JavaScript that can be executed in any JavaScript environment. It is an open source language, and ongoing development is completed in collaboration between Ashkenas and an army of nearly 200 contributors.

CS is designed to let developers write code using a simple and concise syntax. Then to compile into JavaScript code that takes advantage of programming best practices and can be readily executed by any JavaScript runtime environment.

History of CoffeeScript

In December of 2009, Jeremy Ashkenas created a new GitHub repository with an initial commit tagged as "initial commit of the mystery language." This initial commit was rapidly built upon and the first official release of the language, CS version 0.1.0, came less than 2 weeks later on Christmas Eve of 2009.

The CS compiler was originally written in Ruby. However, version 0.5, released in February of 2010, replaced the compiler with one written in CoffeeScript itself. However, there was still a ways to go before the first stable version of CS, version 1.0.0, which was released on Christmas Eve of 2010, approximately one year after the project sprang to life.

CoffeeScript is in version 1.11.0 as 2016 draws to a close, with the latest version having been released in early October 2016. Development of the next generation of CS, CoffeeScript 2.0, is well underway and will be marked by two primary feature additions:

  • Updating the CS compiler to generate modern JavaScript, ES.Next
  • Addition of classes output with the class keyword to CS compiler.

The planned updates to version 2.0 are breaking changes — in other words, code written for CS 2.0 will not be properly compiled by any 1.X compiler. Thus the need for a new major version update to the language. To keep abreast of the latest development work being done on CS, refer to the CoffeeScript 6.0 GitHub repository.

Jeremy Ashkenas

The designer of CS, Jeremy Ashkenas, is an influential and productive JavaScript developer. In addition to CS, Jeremy has developed the following noteworthy projects:

  • LiveScript: a functional programming language with support for object oriented and imperative programming styles that is an indirect descendant from CoffeeScript. Like CS, it compiles into standard JavaScript.
  • Backbone.js: a flexible JavaScript library designed with single-page web apps in mind that emphasizes the separation of application logic code from the code that defines the presentation of application data.
  • Underscore.js: a JavaScript library with utility functions to address common programming tasks.

Jeremy is also a developer for The New York Times as well as a popular speaker for conferences and you will find many videos of his talks on YouTube, such as:

CoffeeScript Syntax

The CS tagline is "Unfancy JavaScript" and that tagline doubles as an effective description of CS syntax. One of the primary goals in designing CoffeeScript was to create a language that was easy to write — modeled after languages like Ruby, Python, and Haskell. For this reason, CoffeeScript abandons many of the syntactical rules that apply to JavaScript.

Most notably, curly braces, semicolons, and the use of assignment keywords like var and function are all omitted in CS. This makes CS extremely concise, but does so at the expense of readability. To get a sense for how this works, consider the following JavaScript function followed by the CS equivalent.

// Function to return the average host rating in JavaScript
var hostRating;
hostRating = function ( totRatingsVal, totRatingsNo ) {
  return totRatingsVal / totRatingsNo;
}

// The same function in CoffeeScript
hostRating = ( totRatingsVal, totRatingsNo ) ->
  totRatingsVal / totRatingsNo

As you can see, the CS version is cleaner, shorter, and contains fewer language operators. This makes the CS version a bit faster to type, but it also masks what is going on behind the scenes to a certain degree.

Literate CoffeeScript

One interesting variation of CS is called literate CoffeeScript. The compiler will recognize this variation as long as the source code file ends with the extension .litcoffee rather than .coffee. Literate CS allows developers to write code using markdown syntax. All writing will be ignored by the compiler as commentary while indented code blocks are recognized as program code.

Let's look at a short example.

## Literate CoffeeScript

[Literate CoffeeScript](http://coffeescript.org/#literate) allows for the use of [markdown](https://daringfireball.net/projects/markdown/syntax#precode) to write CoffeeScript code. All text -- headings, paragraphs, lists, and so forth -- is ignored and treated as commentary by the compiler. 

However, indented code blocks, such as the code block below, are compiled into JavaScript.

  fooBar = ( foo ) -> foo + 1
  doSomething = ( fooBar ) -> alert fooBar
  if fooBar? doSomething

This functionaly creates some interesting scenarios where a CoffeeScript file could be written in markdown and rendered on a webpage as a tutorial while simultaneously serving as a functional piece of program source code.

How To Get CoffeeScript

The CS compiler is typically installed and run using Node.js and CS is available as an NPM package. However, the project can also be downloaded or cloned from GitHub.

If you aren't ready to install CoffeeScript quite yet, you can also give it a spin by heading to CoffeeScript.org and selecting the Try CoffeeScript tab. This tab contains a browser-based CoffeeScript environment. Simply type CoffeeScript on the left and watch it compile into JavaScript in real time on the right.

Resources

If you want to learn CS, the first thing to do is to learn JavaScript. It's best to think of CS as a language designed to make JavaScript developers more productive, enable them to produce fewer errors, and produce JavaScript that adopts best practices.

Since CS compiles to standard JavaScript, debugging CS requires a solid foundation in JavaScript programming. In addition, your CS learning curve will be dramatically reduced if you already know JavaScript since you'll be able to review the compiler output to understand what the CS code is doing.

With that caveat in mind, the following resources will help competent JavaScript developers get up and running with CS in short order.

Tutorials

Books

Summary

CoffeeScript is a programming language with a concise syntax that minimizes the use of the keywords and operators and enables developers to write elegant code quickly. In addition, after being compiled to standard JavaScript, CS code can be executed in any JavaScript-enabled environment.


Further Reading and Resources

We have more guides, tutorials, and infographics related to coding and development:

What Code Should You Learn?

Confused about what programming language you should learn to code in? Check out our infographic, What Code Should You Learn? It not only discusses different aspects of the languages, it answers important questions such as, "How much money will I make programming JavaScript for a living?"