Friday, November 15, 2024

What Is CoffeeScript? – Codecademy Blog

Between Java, JavaScript, and Mocha — there’s a surprising number of programming languages named after coffee. One language you may not have heard of before? CoffeeScript.

The first stable version of CoffeeScript was published by Jeremy Ashkenas in 2010. It started as a small side project that took off quickly. Even Brendan Eich, the creator of JavaScript, said CoffeeScript was a great idea that should serve as an example for the future of JavaScript. And it was.

Today, CoffeeScript isn’t as popular as it once was — but it did have a massive influence on JavaScript, TypeScript, and many of the front-end development processes we still use today.

Learn something new for free

Why was CoffeeScript created?

In 2010, when the first stable version of CoffeeScript was released, JavaScript development wasn’t the same thing as it is today. JavaScript was just JavaScript. There was no TypeScript, no ES6, and nothing that compiled from another language into JavaScript. There weren’t any front-end frameworks. Well, not until the same developer that created CoffeeScript also created Backbone in 2010. (There was jQuery, but that is not really a framework.)

To tell you the truth, JavaScript wasn’t taken seriously. It was mainly used for dynamic effects on a web page. Front-end developers made things pretty with CSS and added effects with JavaScript, usually using jQuery.

Jeremy Ashkenas thought the language had outstanding features and a lot to offer, and with a few changes, he could improve the language. He created CoffeeScript to bring the excellent features of JavaScript to the forefront and hide the more complex and confusing features by adding “syntactic sugar.” Syntactic sugar is syntax used in a programming language to make it easier to read and use. He also made CoffeeScript so it could be compiled into regular old JavaScript, which wasn’t a new concept, but CoffeeScript made it mainstream.

What was CoffeeScript used for?

CoffeeScript was used wherever JavaScript was used by developers and companies that preferred its syntax. When it started, it was mainly used for back-end development, and many of the articles you will find online will say that it is only for back-end development. That is not entirely true. Once developers set up a suitable workflow for compiling the code for the browser, it also worked well for front-end development.

One of the biggest companies that adopted CoffeeScript was Dropbox. They rewrote all of their front-end code using CoffeeScript in one week in 2012. They believed it fixed many of the problems with syntax in JavaScript. After all, Brendan Eich wrote the original version of the language in only ten days in 1995, and there had been few changes to the language up to that point. JavaScript was evolving too slowly, and CoffeeScript added many useful features.

The development team of the Atom text editor, a tool many developers use to write code, also adopted CoffeeScript, making it the official language of their codebase.

Is CoffeeScript dead?

CoffeeScript as a language is not dead. The latest version is 2.6.1 and was released in October 2021, so it is still being developed, and it is a great language to learn. It will teach you a lot about what you can do with a language that can compile to JavaScript, and build any project that you can in JavaScript with it. You may end up even liking its syntax and preferring it.

But the problem is that learning it won’t help you much in the job market, and you won’t find many developers who know or use the language. If you do find a job requiring knowledge of CoffeeScript, chances are you will work on legacy code or convert the CoffeeScript to either TypeScript or JavaScript.

Currently, you can find some jobs on LinkedIn that mention it, mainly at Dropbox. But even they are converting their CoffeeScript to TypeScript. The Atom team converted their codebase to ES6 JavaScript around the same time.

How JavaScript and TypeScript caught up

The first version of TypeScript was released in October 2012. This language was a superset of JavaScript that Microsoft developed. It changed little about JavaScript, only adding types. This meant JavaScript developers could learn TypeScript quickly and write less error-prone code.

Even Dropbox, one of the biggest proponents of CoffeeScript, saw the advantages of TypeScript. By the end of 2015, TypeScript started getting more attention than CoffeeScript.

Another thing happened in 2015, the ES6 version of JavaScript. While it didn’t adopt the unique syntax of CoffeeScript, it adopted many of the features that developers loved about the language. Here are some features that early versions of JavaScript and CoffeeScript have in common:

  • True classes instead of unconventional prototype objects.
  • Arrow functions which made functions less verbose.
  • Using let to define variables.
  • Class inheritance.
  • The in comparison operator.
  • Declaring arrays over multiple lines.
  • String interpolation.

At the time, browsers executed JavaScript differently because JavaScript that worked in one browser didn’t work in another. And very few browsers supported its new ES6 version. But another thing that JavaScript developers learned from CoffeeScript was that you could compile another language to JavaScript.

The Babel JavaScript library was also released around the same time. Babel is a JavaScript compiler that made it possible to write code in the ES6 version and compile it to JavaScript that would be compatible with all browsers. And developers were already familiar with compiling JavaScript after using CoffeeScript and TypeScript. This was a game-changer.

These changes in the JavaScript ecosystem made CoffeeScript a thing of the past, and its adoption started decreasing.

How is CoffeeScript different from JavaScript?

When he developed CoffeeScript, Jeremy Ashkenas drew inspiration from Python, Ruby, and Haskell. The following are some of the current differences:

  • You can name a variable with only the equals sign and don’t have to use var, const, or let.
  • You don’t use semi-colons to end a line.
  • You don’t use curly braces to define code blocks. Instead, you use whitespace (tabs or spaces) like in Python.
  • You can use words instead of symbols for comparison:
    • isnt works for !==
    • is works for ===
    • not works for !
    • and works for &&

There are a few other differences, but over time, JavaScript absorbed many of the features that made CoffeeScript new and different.

What to learn instead of CoffeeScript

While it might be useful to learn CoffeeScript for your own projects, stick with JavaScript if you want to learn a language that is in demand. Our Learn JavaScript course will take you from beginner to JavaScript developer in no time, even if you know nothing about coding.What about TypeScript? It is worth learning, too, because it is in demand and adds the type feature to JavaScript, so you can write more structured code that is less prone to errors. Once you know JavaScript, TypeScript is easy to pick up, and our Learn TypeScript course will teach you what you need to know to be proficient in this language.

This blog was originally published in February 2023 and has been updated to clarify details about JavaScript features.

Related Articles

Latest Articles