Do you struggle with Braces and semicolon in writing code of JavaScript. There is a solution of this problem and that is – CoffeeScript. In this post I’ll explore CoffeeScript – a minimalistic language that compiles to JavaScript.
CoffeeScript – The Awesome way to write JavaScript
CoffeeScript is a little language that compiles into JavaScript. Underneath all those awkward braces and semicolons, JavaScript has always had a gorgeous object model at its heart. CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way.
The golden rule of CoffeeScript is: “It’s just JavaScript“. The code compiles one-to-one into the equivalent JS, and there is no interpretation at runtime. You can use any existing JavaScript library seamlessly from CoffeeScript (and vice-versa). The compiled output is readable and pretty-printed, passes through JavaScript Lint without warnings, will work in every JavaScript runtime, and tends to run as fast or faster than the equivalent handwritten JavaScript.
The language adds syntactic sugar inspired by Ruby,Python and Haskell. NO braces and semicolons. Since March 16, 2011, CoffeeScript has been on GitHub‘s list of most-watched projects, and as of 29 August 2012 is the eleventh most popular language on GitHub. CoffeeScript compiles predictably to JavaScript and programs can be written with less code, typically 1/3 fewer lines, with no effect on runtime performance
Syntax of CoffeeScript – Easy to use – Easy to Read
Here I am showing some example.
1. Assignment
str = "CoffeeScript"
2. Function
square = (x) -> x * x
3. Condition
string = "Passed" if condition
4. Multiply numbers with 2
[1..10].map (i) -> i*2
5. Object
math =
root: Math.sqrt
square: square
cube: (x) -> x * square x
6. Loop
eat food for food in ['toast', 'cheese', 'wine']
Installation
The CoffeeScript compiler is itself written in CoffeeScript, using the Jison parser generator. The command-line version of coffee is available as a Node.js utility. The core compiler however, does not depend on Node, and can be run in any JavaScript environment, or in the browser.
To install, first make sure you have a working copy of the latest stable version of Node.js, and npm(the Node Package Manager). You can then install CoffeeScript with npm:
npm install -g coffee-script
(Leave off the -g if you don’t wish to install globally.)
If you’d prefer to install the latest master version of CoffeeScript, you can clone the CoffeeScriptsource repository from GitHub, or download the source directly. To install the lastest master CoffeeScript compiler with npm:
npm install -g http://github.com/jashkenas/coffee-script/tarball/master
Or, if you want to install to /usr/local, and don’t want to use npm to manage it, open the coffee-script directory and run:
sudo bin/cake install
For More Information and learning
1.http://coffeescript.org
2.http://jashkenas.github.com/coffee-script/documentation/docs/grammar.html