What Is CoffeeScript and Is It for You?

I have noticed a lot of buzz among new developers who are interested in CoffeeScript. So, I set out to create this post where I will briefly introduce CoffeeScript and tell you a little bit about it, what its best assets are and who it is best for. I hope that this post will give you a general idea about this language and let you decide if this is perhaps something for you – or not for that matter.

What is CoffeeScript, exactly?

In the shortest of explanations, CoffeeScript is a programming language that compiles into JavaScript. What this means is that you write the code in CoffeeScript, it is run through a compiler, and it outputs into JavaScript that is free to use by a browser. CoffeeScript has been inspired by Ruby, Python and Haskell in order to enhance JavaScript by writing faster and more legible code. It has been proven that by writing CoffeeScript you write about a third fewer lines than you would writing in JavaScript.

CoffeeScript is action filled with amazing features, most notably pattern matching and list comprehension to make it an easier and, therefore, appealing language. CoffeeScript is wonderful because it integrates easily with other JavaScript libraries. This language has had such a great effect on the development community – Ruby developers tend to love CoffeeScript because of its syntax especially – that even Brendan Eich admitted that CoffeeScript had influenced him on the future of JavaScript.

How does it differ?

Like I said, CoffeeScript is all about efficiency so it doesn’t have any braces or semicolons in its syntax. In all honesty, CoffeeScript is not meant to replace JavaScript but build onto it; CoffeeScript’s motto is “It’s just SavaScript.” It is seen as a nicer way to write JavaScript; a more concise way. The best way for me to convince you of this is to show you the actual code so you can clearly see for yourself.

Below are pieces of a chart taken right from CoffeeScript’s website which shows you the differences between the two languages at the basic level.

CoffeeScript Examples

Why CoffeeScript?

Believe it or not, in the early days of JavaScript it did not have as good of a reputation as it does now. However, JavaScript has come a long way by now with many abilities and features, if you will. It was simply rough around the edges in its early days. What CoffeeScript does is make use of JavaScript’s capabilities; it allows you to write powerful JavaScript code without having to know a lot of it in great detail by adding syntax sugar. Syntax sugar is the term used for syntax within a programming language which makes thing easier to read or express; it is designed to make thing easier and therefore better for you. It makes the language ‘sweeter’ to our eyes as the code is clearer and more concise.

The most notable aspect of CoffeeScript is its lack of semicolons or braces. This is very much like Ruby and Python which is why the developers from those communities are fond of CoffeeScript. This obvious simplification contributes greatly to writing less code. Getting rid of this visual clutter is just heavenly and one of the biggest factors people enjoy about using CoffeeScript. Additionally, CoffeeScript actually adds keywords, classes and features – such as heredocs – which make coding easier. I cannot stress enough how CoffeeScript is truly about efficiency.

Who is CoffeeScript for?

A lot of experienced JavaScript and CoffeeScript developers seem to agree that CoffeeScript is not just for anyone and it is also not for everyone. CoffeeScript is mostly meant for people who have some experience using JavaScript – no need to be an expect by any means. This way though, you will be able to appreciate JavaScript’s capabilities and limitations and embrace them with CoffeeScript. CoffeeScript is about writing what you were going to in JavaScript in a more fluent way.

Not that you shouldn’t, but you would have a hard time writing and comprehending what is going on in CoffeeScript without a solid foundation of JavaScript. This holds especially true if you are using other JavaScript libraries and also because all the debugging of CoffeeScript is done through JavaScript. It is not recommended to write CoffeeScript without knowing JavaScript to some extent; but at the same time it is common for you to learn both at the same time because those two go hand in hand so well.

However, if you are fairly fluent in JavaScript, many say that there is actually no need for you to learn CoffeeScript as you can get around JavaScript like nobody’s business. At this point you should not be learning CoffeeScript to replace JavaScript.

Final thoughts

At the end of the day it doesn’t matter what others say it matters if you yourself prefer CoffeeScript. I hope this post was a good introduction to what CoffeeScript is all about and that you have a pretty decent idea whether or not this language is something for you.

Advertisement