Hello, Guys, I learned a new language and I am going to share and tell you how it works.
What is CoffeeScript?
CoffeeScript is a little language that compiles into JavaScript. It is an attempt to expose the good parts of JavaScript in a simple way yet retaining the flexibility and beauty of the language. The golden rule of CoffeeScript is: “It’s just JavaScript”.
How to Install CoffeeScript?
You require node.js in your system which you can download from https://nodejs.org/en/.
Then install npm, that you can download from here https://www.npmjs.com/.
Run this command
$ npm install -g coffee-script
You can also install it globally by using this command line
npm install --global coffeescript
and locally in a folder using this
npm install --save coffeescript
Advantages
- No variable declaration required
- Least amount of code to solve problems
- Readable and Understandable
- Easy to Maintain
- No semicolons or parenthesis required
- Reliable
- Class-based inheritance
- Extensive library support
How does it work?
We use indent to give our code a better visual appearance. But CoffeeScript takes this convention and makes it part of the language to tidy up all those curly braces. You might have seen indent in the above CoffeeScript code.
Indentation adds to the readability of CoffeeScript and it removes lots of the unnecessary braces.
In CoffeeScript, the arrow ->
is used to define a function, with the parameters in brackets in front. If a function has no parameters, the brackets are optional. You will see it below in the example.
Classes in CoffeeScript
Most of the programming language like java, PHP C# has a class keyword to define a class. JavaScript is a language limited by few rules and its ability to define OOP classes in many ways is powerful, yet confusing to many. CoffeeScript solves this by adding the class keyword
CoffeeScript Classes Example
class Cars constructor: (@carname) -> hello: -> console.log "New #{@carname}"
Now JavaScript Compilation of above CoffeeScript
var Cars; Cars= (function() { function Cars(carname) { this.carname= carname; } Cars.prototype.hello = function() { return console.log("New" + this.carname); }; return Cars; })();
Here some code below in CoffeeScript
Just alert in coffeescript alert "Hello CoffeeScript!" Message in Variable message = "First code" alert message Function in coffeescript coffee = -> newm = "Hello" "Message printed is " + newm alert coffee() Sum of two numbers sum = (a,b)-> a=2 b=5 a+b alert(sum())
Here is the JavaScript version of above CoffeeScript
var coffee, message; Just alert alert("Hello CoffeeScript!"); Message is Variable message = "First code"; alert(message); Function coffee = function() { var newm; newm = "Hello"; return "Message printed is " + newm; }; alert(coffee()); Sum of two numbers sum = function(a, b) { a = 2; b = 5; return a + b; }; alert(sum());
Try Above code here.
List of command for command line
-b, --bare compile without a top-level function wrapper -c, --compile compile to JavaScript and save as .js files -e, --eval pass a string from the command line as input -h, --help display this help message -i, --interactive run an interactive CoffeeScript REPL -j, --join concatenate the source CoffeeScript before compiling -m, --map generate source map and save as .js.map files -M, --inline-map generate source map and include it directly in output -n, --nodes print out the parse tree that the parser produces --nodejs pass options directly to the "node" binary --no-header suppress the "Generated by" header -o, --output set the output directory for compiled JavaScript -p, --print print out the compiled JavaScript -r, --require require the given module before eval or REPL -s, --stdio listen for and compile scripts over stdio -l, --literate treat stdio as literate style coffee-script -t, --tokens print out the tokens that the lexer/rewriter produce -v, --version display the version number -w, --watch watch scripts for changes and rerun commands
By now you might have realised that writing code in Coffeescript is much easier than a regular JavaScript. I have not given complete details about CoffeeScript, I will write another post on that, but for now, head to the official site and learn CoffeeScript.
One Reply to “CoffeeScript “It is JavaScript””