The Basics Of Using Coffeescript For Writing Javascript

JavaScript is a programming language that enables learners to develop web pages easily and add interactive features. JavaScript’s popularity has increased in recent years with the introduction of advanced internet applications. However, the language has its own peculiarities and often these can crop up from nowhere to trouble you. The latest solution to all these problems is CoffeeScript.

What is a CoffeeScript?

CoffeeScript is a small programming language that gets compiled to JavaScript. It tries to make writing JavaScript code better by providing you with a more reliable and to the point sentence structure as well as avoiding the bad parts and odd nature of the JavaScript language.

CoffeeScript is an effort to bring on the surface the good parts of JavaScript in a simple way.

CoffeeScript was introduced to the world by developer Jeremy Ashkenas.

Why CoffeeScript?

In earlier days JavaScript did not have good reputation due to the copy-and-paste mentality of users. It appeared that only a few had any idea what was going on. It was considered as a not so important language. However, JavaScript as a language has a lot to offer and has some super features.

CoffeeScript helps to use the powerful features of JavaScript without learning the less known features of JavaScript.

This means with the CoffeeScript you can write fewer codes and do things faster. It also makes it easier to read and maintain.

Example of CoffeeScript Code

For understanding, it let us write a small clip of JavaScript and then write the same clip in CoffeeScript.

For example we will write Four Badminton playersinside the <body> tag of the web page.

JavaScript:

$ (function ( ) {

$ (“body”) . html (“ Four Badminton players”);

})

CoffeeScript:

$ — >

$ (“body”) . html “ Four Badminton players”

Thus, CoffeeScript helps you to write JavaScript quicker due to its simplified sentence structure.

Here are two basic rules to remember:

There are no curvy brackets like {[clip]}) in CoffeeScript. Instead, indentations are used.

Functions that use statements do not use brackets (e.g.(statement)) around them as shown in the code above. You can use them if you want but most of the time you can avoid them.

Comparison of JavaScript and CoffeeScript

As shown in above example, we will look at some differences between JavaScript and CoffeeScript.

Firstly the clip( ) { }has been replaced with $ – >. Thus first line is written as $ ->, which is called as jQuery.

If you wanted to state a clip in JavaScript, it would be as:

clip say ( and) {

alert(abcd);

}

But in CoffeeScript it would be as:

say = (abcd) – >

alert and

You will note that we can avoid using (;). CoffeeScript adds them when compiles.

Another noticeable thing is that you no longer use var statement which is required to use when writing in JavaScript to avoid creating global variables.

So instead of:

var x=7

We can just write:

x=7

Conclusion

All of this may seem difficult initially to get familiar with, especially if you are used to a programming language like PHP, that use brackets ({}) to enclose a clip block and semicolons (;) at the end of a clip. But it is much faster to type, and with practice, it becomes natural.