CoffeeScript | Introduction

CoffeeScript is a lightweight language that compiles into JavaScript. It provides simple and easy to learn syntax avoiding the complex syntax of JavaScript. CoffeeScript is influenced by JavaScript, Ruby, YAML, Haskell, Perl, 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 assignment.

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.

Advantages of CoffeeScript:



  • Easy to Understand: The syntax of this language is a simple form of JavaScript. The simplicity of the syntax is the beauty of this programming language. The code written in CoffeeScript is very clean and easily understood.
  • No var keyword: Unlike JavaScript, there is no need to use the var keyword before declaring a variable. Thus, it helps to avoid scope declaration issues in a program.
  • No symbols: The symbols like semicolons, parenthesis, and curly braces do not play any role in CoffeeScript. Instead of these, whitespaces are used to differentiate the code inside functions, loops, etc.
  • Less Code: In comparison to JavaScript, the lines of code reduces to half. Benefit, less code reduces the complexity of the program.
  • Maintainable: CoffeeScript provides the concept of aliases for many operators which makes code understandable easily, readable. It becomes easy to maintain programs written in CoffeeScript.
  • Reliable: CoffeeScript is a reliable and safe programming language to make dynamic web pages.

CoffeeScript uses JavaScript libraries and vice-versa that provides a vast range of libraries when working with CoffeeScript. The filename extensions of this language are .coffee, .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.

filter_none

edit
close

play_arrow

link
brightness_4
code

console.log "Hello world"

chevron_right


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:

  • Significant whitespace
  • Straight forward class definitions and functions
  • Lambda functions

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.

full-stack-img

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.