I initially dismissed CoffeeScript as just another silly attempt at making JavaScript not be JavaScript. It probably also compiled the code into some sort of totally wonky JavaScript, too.
However, after thinking about it for a while, I decided to actually try CoffeeScript and see for myself. Now, after using it quite extensively on some projects, I feel I’m qualified to give my completely unbiased, 100% accurate and correct opinion on it.
How CoffeeScript compares to JavaScript
CoffeeScript is actually a quite neat language. It uses indentation instead of braces, making it kind of Python-like, and various other niceties, which remind some other languages like Ruby.
Using it feels like you need to write much less code to achieve the same results, especially when working with lots of callbacks or in functional style code (array map, filter, etc.).
I’m not going to dive deeply into CS syntax here, but consider the following which demonstrates this quite nicely:
itemIds = items.map (i) -> i.id
vs.
var itemIds = items.map(function(i) { return i.id; });
Although the CS example above contains some syntax which may look a bit odd at first, it’s much, much less “noisy”, letting you see what’s happening more easily with a glance.
In addition to just the syntax, it includes many useful language-level features for things like for-each loops, “splats” or binding variables from arrays or objects.
And, believe it or not, the compiled JS code it produces is actually quite tolerable and follows good conventions.
CoffeeScript does not change JavaScript into something else
Although CoffeeScript works in places to hide some of JavaScript’s perceived ugliness, it still is not a complete replacement.
It does not even attempt to entirely hide JavaScript from you: To be able to use CoffeeScript, you also need to know certain things from its JavaScript roots.
Two primary examples are closures and the binding of this
Binding of ‘this’
CoffeeScript provides a convenient way to bind anonymous functions into the current scope, using the =>
operator when declaring the function.
What this does is essentially the same as doing someFun.bind(this)
. (It does not compile to that exact code though).
However, without knowing how JavaScript’s scope binding works, you would not know when, why or how to use =>
Closures
Everyone who has used JavaScript has probably fallen victim of the closure in a loop problem.
Contrary to what you might expect, this same thing is present in CoffeeScript.
I spent a good 30 minutes debugging some CS code I wrote, only to realize that this was the cause. Imagine my headdesking.
Essentially the fact I was using CS made me completely oblivious to the cause of the problem. If it had been JavaScript, I would’ve spotted it much sooner.
So what is the point of CoffeeScript
To make writing JavaScript more fun!
Considering CoffeeScript lets me write more concise code, provides a bunch of nice features and is overall a quite well thought out language, it does indeed make writing JavaScript more fun to me.
Since it does not attempt to hide JavaScript from me behind some fancy schmancy syntax, it’s still ever as flexible as JavaScript is, letting you do confusing things with closures and mess with prototypes and whatever.
So it’s not is a replacement for knowing JavaScript. If you’re a beginner in JavaScript, I would suggest you learn to be good in JavaScript first, and then you can consider learning CoffeeScript if it suits your purposes.