CoffeeScript

A (kick ass) little language that compiles into JavaScript.

JavaScript is kind of a big deal.

So what the hell is CoffeeScript?

Why would I want to learn CoffeeScript?

Installing CoffeeScript

With Homebrew (preferred method)

If you don’t already have Homebrew, download it; It’s awesome. http://mxcl.github.com/homebrew/

With Node(JS) Package Manager

If you want to install NodeJS and NPM without Homebrew you can install NodeJS (https://github.com/joyent/node/wiki/Installation) and NPM (http://npmjs.org/)

TextMate Support

The official CoffeeScript TextMate bundle can be found at https://github.com/jashkenas/coffee-script-tmbundle

Writing CoffeeScript

Think JavaScript without the countless semi-colons and brackets. CoffeeScript uses whitespace (or indenting) to define blocks of code. You also don’t need to use parentheses to invoke a function when passing arguments or use the keyword var when declaring variables. The keyword function has also been replaced by ->.

Basic CoffeeScript Syntax

Let’s convert a simple JQuery click method implementation from JavaScript to CoffeeScript:

Convert Simple JavaScript to CoffeeScript

example.coffee -> examples.js

CoffeeScript

Compile the CoffeeScript using the terminal.app coffee -c example.coffee

Compiled JavaScript Rendered JavaScript

A few CoffeeScript features

Compiling CoffeeScript:

Like Compass or Sass, you’ll be compiling your CoffeeScript files to JavaScript with the terminal.app. Here are some methods of compiling:

Coffee in the terminal

Coffee is also available in the terminal in more ways than simply a compiler.

CoffeeScript in the terminal

For the curious ones…

Much like the inventor of JavaScript took initiative to “hack together” JavaScript in under 10 days so there will be a viable competitor to Java for client side programming; Jeremy Ashkenas, creator of CoffeeScript, took initiative to create a “better javascript” because he couldn’t simply wait for someone else to do it for him. And like Sarah Conner says, “No fate but what we make.” -Terminator II

“It helps to have things like CoffeeScript out there it isn’t overriding, it doesn’t tell us what we must do but its suggestive, and if we want to pave that cow path we can, and I’m in favor.”

-Brendan Eich (The creator of Javascript)

“I think CoffeeScript is clearly good stuff. CoffeeScript is elegant it sort of takes the good parts, removes all of the stupid awful syntax that were inherited from the wrong languages, replaces it with something that is small and elegant and expressive. CoffeeScript is really great. And CoffeeScript is having a big influence on the ECMA Script committee… CoffeeScript is definitely in the right direction and I would like to see future languages looking more like CoffeeScript than like C.”

-Douglas Crockford (Inventor of JSON and JSLint among other JavaScript best practices and general bad assery)

“Serious business”

I’ve written a more verbose version of this article and posted it for people who may be interested in a more “serious business” introduction to CoffeeScript; a brief history of JavaScript and where, and why, CoffeeScript is relevant for web developers. Check it out at https://gist.github.com/1374768

More information on learning CoffeeScript

Projects using CoffeeScript