“CoffeeScript, c’mon and/ take a sip, they call it/ CoffeeScript, Coff-”
- oh, sorry, didn’t see you there! I was just humming the jingle from CodeSchool’s CoffeeScript course. Been working on it for a few days. I’ve never been a huge fan of the brown caffeinated stuff, but CoffeeScript? I’m addicted.
CoffeeScript is described by its creator, Jeremy Ashkenas, as “a little language that compiles into JavaScript.” Here’s how I would describe it: “JavaScript is written to give you a brain hemorrhage. CoffeeScript is more like a mild migraine.”
Essentially, CoffeeScript is a language that compiles 1-to-1 to JavaScript, but is more clear and less error-prone. Forget the dreaded var
keyword; it has no place in the wonderful world of CoffeeScript, because it’s added automatically. I like semicolons in English sentences; however, in the world of JavaScript, it’s difficult to remember every semicolon. Thankfully, Automatic Semicolon Insertion will usually take care of it, but it can lead to unexpected results. With CoffeeScript, no more semicolons! And those pesky braces ({}
) appearing all over the place? Gone!
Now, some would say, “Hey, what are you doing with my beautiful JavaScript? It was so precise, so explicit!” I would tell them to go try Ruby and understand that extraBoilerplate !== greaterPrecision
(I wrote that in JS so they would understand). Or, as you would write it in CoffeeScript, extraBoilerplate isnt greaterPrecision
. (Yes, the ‘ was left out of isnt on purpose – that’s how it’s spelled in CoffeeScript.)
Just to demonstrate how awesome CoffeeScript is, I’m going to write the rest of this post in CoffeeScript to show that it’s really much simpler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
|
Still JS-y syntax (it’s just impossible to escape), but isn’t it at least a bit clearer?
Hopefully this has been a pleasant little introduction to CoffeeScript. You can run the code on CoffeeScript’s website here. (Click “run” on the top-right, and watch what happens in your console!) And while you’re there, read up on the language and all its features, try writing some of your own in the “Try CoffeeScript” modal, and maybe even take the CodeSchool course. You won’t regret it!
P.S. Bonus extra life!!! Check out some real-life jQuery/CoffeeScript action in the code for the Flatiron Showcase website. I have some simple DOM manipulation stuff that I just converted to CoffeeScript yesterday. Check it out on Github!