CoffeeScript is a small programming language that compiles into JavaScript. It seeks to make writing JavaScript code better by providing you with a more consistent and succinct syntax, as well as by avoiding the “bad parts” and quirky/irregular nature of the JavaScript language.

Coffee Script is an attempt to expose the good parts of JavaScript in a simple way. The code compiles one-to-one into the equivalent JS, and there is no interpretation at runtime. Coffee Script does not have the semi-colons and curly braces, similar syntax to the likes js. This means you can write less code and do things faster. It also makes it easier to read and maintain

Why coffee Script

  • variable safety
  • loops and comprehensions
  • easy safe loops using has Own Property: for own x of obj
  • string interpolation: “My name is #{name}”
  • easy closures (do (x) -> …)
  • ranges: for i in [0..10]
  • range slicing: myArray[2..5]
  • existential operator: if x? then use(x), are?.you?.there?
  • readable regular expressions
  • De-structuring assignment: [a,b] = [x,y]
  • binding: => this.foo()
  • Number of lines of code has been shown to be one of the few good indicators for predicting defects. Coffee Script significantly reduces the number of lines of code in a project and therefore the number of defects.
  • Coffee Script improvements: indenting two spaces and using -> plus no closing parents and curly braces makes nested callbacks much easier to read and write
  • When discussing Coffee Script, you need to keep in mind that it is not a replacement for JavaScript. Coffee Script is a “JavaScript generator.” Your browser doesn’t need any new plug-ins or add-ons to utilize Coffee Script.
  • This also means you can use all your favorite JavaScript libraries (jQuery, Knockout.js, Sammy.js and so on) with Coffee Script (and vice versa).
  • With the proper use of whitespace you can make your script more readable as well as maintainable.

How CoffeeScript Works

The code writing and use process of CoffeeScript is simple:
1.Write your code in a .coffee file
2.Compile it into a .js file
3.Include the .js file in your web page/s like you would any other JavaScript file

JavaScript Syntax vs. CoffeeScript Syntax

Variables

In javascript

var message;
message = "Ready for some Coffee?";
alert(message);

In Coffee script

message = "Ready for some Coffee?"
alert(message)

No variable declarations
No semicolons

Advantages

In 2013 it was also ranked 29th among languages, based on number of questions tagged at Stack Overflow.

  • Coffee Script has much nicer, more Ruby-like syntax than JavaScript.
  • JavaScript does some very silly things, while Coffee Script has several safeguards to prevent unwanted behavior. (For example, if you forget to use the “var” keyword to define new variables in JavaScript, they will suddenly get global scope with no warnings! Coffee Script protects you from this behavior).
  • Coffee Script should work nicely with jQuery
  • Python style white spacing
  • Ruby styled lightweight syntax
  • Concise function declarations
  • Class based inheritance

Disadvantages

  • Difficult to learn.
  • Switch from JavaScript to coffee script not easy.
  • Coffee script was designed in the vein of Python and Ruby, so if you hate those languages then you will probably hate coffee script.
  • Its compile every time.
  • You’ll need a basic knowledge of JS for debugging purposes. You can’t directly start here, naturally.
Show CommentsClose Comments

Leave a comment