What is CoffeeScript all about?
Curious about CoffeeScript, are you? This should give you a quick idea of what it’s all about! Our lead developer Paul Annesley and I took a break from the daily grind here in the SitePoint offices to put this video together for you.
CoffeeScript is rumored to be made by people who dislike JavaScript syntax for people who equally dislike JavaScript syntax. It might even go so far as to help people who don’t understand how JavaScript works at its roots to write more optimized code, since the compilation into JavaScript is implemented pretty well from a performance point of view. Would you agree?
Check out the video and let us know your thoughts!
Test your CoffeeScript knowledge with our new Quiz here
Buy our latest Jump Start book on CoffeeScript
JGB: I’m here with Paul Annesley, who is the lead developer at the SitePoint Group. Paul is going to introduce us to CoffeeScript. So, what exactly is CoffeeScript?
Paul: CoffeeScript is a programming language which compiles to JavaScript. That means that you write your code in CoffeeScript, run it through the compiler, and the output is JavaScript that can
be served up to your browser.
JGB: If I’m a JavaScript programmer, why would I want to use CoffeeScript instead?
Paul: CoffeeScript lets you clearly express your program with a lot less code than JavaScript. It’s got a lot of sort of lightweight add-ons like Ruby style string interpolation and Python style list comprehension. It makes a lot of common tasks much easier than JavaScript. Pass around lot of functions, so CoffeeScript provides a very brief way of expressing those.
It also brings a full class-based object system. JavaScript is generally prototypical, which confuses the hell out of most people. So that sort of class system familiar to Ruby and Python and Java developers makes things easier.
JGB: Are there particular types of JavaScript development that would benefit from CoffeeScript?
Paul: Personally, I use CoffeeScript for web development, game development and anything that I want to end up in JavaScript. Regardless of what libraries or platforms you’re building for, the output of CoffeeScript is JavaScript, so there’s nothing that it can’t do.
JGB: Are there downsides to using CoffeeScript?
Paul: The JavaScript served up to the browser is not the CoffeeScript that you write, so that can make runtime debugging more difficult. Personally, I haven’t found that to be a problem. The CoffeeScript compiler writes really clean, easy to read JavaScript, so I haven’t had a problem. There is an experimental technology called Source Maps and that’s said to make it even easier to do that runtime debugging.
JGB: How do I get started with CoffeeScript, then? Do I need to install anything to use it?
Paul: If you are using Rails 3.1 or newer, then CoffeeScript comes out of the box, so just enable the asset pipeline, put .coffee
on the end of your file names, and Rails does the rest. If you’re using PHP or Django, or any other system — web, node, or otherwise — it’s easy enough to get up and running on your development environment.
So, personally, I use Homebrew on my Mac to install Node.js and then use the node package manager, or NPM, to install CoffeeScript. That keeps a command line tool, which will turn your Coffee files into JavaScript.