CoffeeScript “It is JavaScript”

Page Visited: 3834
Read Time:3 Minute, 24 Second

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.

About Post Author

Girish

Hello Guys I am a website developer by profession but is always keen on learning new things. I have been investing in Mutual funds, stock market for the past few years because of which I have gained good knowledge. I started my entrepreneur journey in 2019 which lead me to learn more things as I am moving forward. I always love to share whatever I learn. Always had a craze for cars from my childhood, which inspired me to start this website.
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Girish

Hello Guys I am a website developer by profession but is always keen on learning new things. I have been investing in Mutual funds, stock market for the past few years because of which I have gained good knowledge. I started my entrepreneur journey in 2019 which lead me to learn more things as I am moving forward. I always love to share whatever I learn. Always had a craze for cars from my childhood, which inspired me to start this website.

One thought on “CoffeeScript “It is JavaScript”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enable Notifications OK No thanks