Open In App

CoffeeScript | Introduction

CoffeeScript is a lightweight language that compiles JavaScript. It provides simple and easy-to-learn syntax avoiding the complex syntax of JavaScript. CoffeeScript is influenced by JavaScript, Ruby, YAML, Haskell, Perl, and Python and has influenced MoonScript, LiveScript, and JavaScript.

In this tutorial, the readers will learn the basic functionality of CoffeeScript with code to build dynamic websites and web applications after mastering it.



Before going through this tutorial, the readers should have prior knowledge of JavaScript as it is similar to CoffeeScript. It contains additional features list comprehension and destructing assignments.

History: The CoffeeScript language is designed by Jeremy Ashkenas. It was first introduced on 13 December 2009 on the git when Jeremy Ashkenas made the first git commit of CoffeeScript. On February 21, 2010, the first pure CoffeeScript compiler was released earlier the CoffeeScript compiler was in Ruby language. By that time the GitHub project attracted many contributors to GitHub making it the most demanded project of that time.



Installation

Prerequisites: You should’ve installed Node.js and NPM before running the commands below and installing CoffeeScript.

# Install locally for a project:
npm install --save-dev coffeescript

# Install globally to execute .coffee files anywhere:
npm install --global coffeescript

Advantages of CoffeeScript: 

CoffeeScript uses JavaScript libraries and vice-versa that provide a vast range of libraries when working with CoffeeScript. The filename extensions of this language are .coffee, and .litcoffee. 
To execute a CoffeeScript file, you need to type the following command: 

coffee -c filename.coffee

The syntax is not quirky. For example, if you want to print something on the console then you just need to type it inside console.log without parenthesis as shown below. 




console.log "Hello world"

Benefits of CoffeeScript: The benefit of learning CoffeeScript is that you will explore its vast libraries including JavaScript libraries. CoffeeScript contains three things that every programmer wants: 

Limitations: The only limitation of CoffeeScript is that it is very sensitive to whitespaces. If the proper indentation is not maintained, the error will be thrown. So, programmers need to be very careful with whitespaces.

CoffeeScript vs Typescript:

CoffeeScript TypeScript
It is best used when we want more readable code and lots of syntactic sugar. It is best used when we want optional static typing and better tool support.
It is used for server-side web applications only. It is used for both server-side and client-side web applications.
It is developed and maintained by the Open Source developer community itself under the MIT License. It is developed and maintained by Microsoft under the Apache2 License.

Is CoffeeScript dead?

With the emergence of ES6 in 2015 as well as new languages, which can be trans compiled to JS, a lot changed to CoffeeScript’s disadvantage. It started facing serious competition. The main problem was not that it was bad, but it was not good enough. In ES6, JavaScript also added many powerful new features and also had a larger community.  As of today, January 2021, CoffeeScript is almost dead on the market. 

Article Tags :