Exploring CoffeeScript Part 1 – And Then There Was Coffee

June 21st, 2011

Those of you who have been reading this blog lately know that I’m quite enthusiastic when it comes to JavaScript. As the common behavioral language of the web, JavaScript has been making its way into other aspects of application programming over the last couple of years, notably with server-side frameworks like Node.js and NoSQL databases like CouchDB. I personally find JavaScript to be an awesome programming language, especially when you stick with the good parts. So I’m more than pleased that JavaScript is finally making its way out of the browser to fulfill other purposes as well.

But a couple of months ago I ran into another programming language called CoffeeScript. This neat little language, created by Jeremy Ashkenas, simply compiles its syntax into JavaScript code. In fact, there’s no interpretation of CoffeeScript code at runtime. CoffeeScript essentially builds on top of JavaScript as the core language while adding some syntactic niceties that are heavily inspired by Ruby. The CoffeeScript compiler emits very clean JavaScript code that not only follows best practices but also complies to JSLint without warnings. This all basically means that wherever you can use JavaScript, whether it’s in the browser, on the server-side or for a particular database, you can use CoffeeScript as well. In fact, you can use any JavaScript library right in your CoffeeScript code!

But why should one use CoffeeScript over JavaScript? Well, for starters, the CoffeeScript syntax is much more concise than its JavaScript equivalent. There’s a lot less typing involved for getting the equivalent JavaScript code. Check out this example on the CoffeeScript wiki.

Second, the CoffeeScript language is much richer than JavaScript. It add splats, classes, destructuring assignments and many more small details that makes your code more readable and fun. 

And thirdly, because the creator of Ruby on Rails, David Heinemeier Hansson, says so :-). The upcoming 3.1 release of Ruby on Rails will include CoffeeScript in the box, which means that other web frameworks will probably also add support for CoffeeScript in the future.

Enough of the marketing talk already. Let’s have a quick look at how to get CoffeeScript up and running. For starters, you need to have Node.js  and npm installed. Check out the installation guide or Matthew Podwysocki’s on how to get Node.js up and running on Windows using Cygwin.

When you have node.js all set up, you can simply use the following command to install the CoffeeScript compiler using npm:

npm install -g coffee-script

Now that we have CoffeeScript installed, we can compile .coffee files into .js files or we can use the interactive REPL. Let’s go for the canonical “Hello Pluto” example here and create a file named hello.coffee with the following code:

console.log 'Hello CoffeeScript'

Notice that we don’t have to provide parentheses here in order to call the console.log function. Next we can use the following command to print the compiled JavaScript directly to our console window:

coffee -p hello.coffee    

or we can use this next command to compile our hello.coffee file to a corresponding hello.js file at the same location in the file system:

coffee -c hello.coffee

The compiled JavaScript output looks like this:

(function() {
  console.log('Hello CoffeeScript');
}).call(this);

You might wonder why the call to console.log is wrapped into a self-executing function. The reason for this is to prevent naming collisions when using our code in conjunction with other JavaScript libraries. You can tell the CoffeeScript compiler to omit this self-executing function by specifying the –b, –bare command-line option.  

We’re also able to keep the CoffeeScript compiler running with the –w, –watch command-line option. This basically lets the CoffeeScript compiler watch for the specified .coffee files and recompile them as soon as they’re changed.

coffee -cw hello.coffee   

So these are the very basics on how to get up and running with CoffeeScript. Expect to see more of the interesting stuff that this amazing programming language has to offer as I’m going to be writing a couple of more blog posts about CoffeeScript. I certainly recommend that you have a decent knowledge of JavaScript before you pick up CoffeeScript. Being able to understand the JavaScript code that is emitted by the CoffeeScript compiler can be crucial at times.

Until next time.

  • Adam Cassel

    sweet! thanks for the great introduction Jan. 

    your efforts are greatly appreciated by the community!

    very much looking forward to your coming tutorials! 

  • Brian M

    Looking at the language in the sample it just begs the question why do people keep creating cryptic style languages!
    Oh for a language with a readable syntax and comments that compiles to javascript but comes from a sensible easily readable (read maintainable) language . Or are  language  designers so full of their own ‘cleverness’ that that are incapable of designing a sensible language! 

    • Seth Paulson

      What exactly don’t you like about the syntax? I think it looks much better than javascript myself.