DEV Community

loading...
Cover image for Why I LOVE CoffeeScript!(and why you should use it)

Why I LOVE CoffeeScript!(and why you should use it)

seanolad profile image Sean ・2 min read

So Why CoffeeScript?

Have you ever wanted JavaScript to be easier. I know many different frameworks offer something, like Vue for better visuals, or React with virtual DOM(which I do recommend you check out), but, TBH, simplicity is always lacking. If we're being honest sometimes it's a real issue, and after a while you just get used to it. No more, because that's what CoffeeScript is for.

So what is Simple when it comes to programming with CoffeScript?

Most code newbies start with readable programming languages like, Python or Ruby. JavaScript is likely up there with the top ten, but even so, the gap in readability between Ruby and Vanilla JS is too big. So some genius put the best of both worlds together and got Coffescript. Now someone might say:

I like JavaScript just fine.

And that's fine, but once you see the drastic difference in readability I think it's fair to say you will want to use CoffeeScript for its readability.

Here's this example:

CoffeeScript:

Vanilla JS:

Okay, but doesn't all the interpretation of the more readable code make running code on CoffeeScript slower?

No, luckily for us CoffeScript code is compiled to a JS file before it's interpreted using JIT.

CoffeeScript 2.5.1, the newest version, is supported in Node.js. CoffeeScript supports all ES6 additions. And for pre-Ruby developers CoffeeScript reads like Ruby with the addition of JavaScript concepts, and CoffeeScript is great for web development.

So why haven't I seen anything using CoffeScript? or at least heard about it?

CoffeeScript isn't as popular as languages like TypeScript, which many JavaScript programmers flock to. There are many factors that go into this, it's mostly preference and usage, but also how long it takes to learn. You see all JavaScript code is valid TypeScript code but not neccesarily vice versa. So in essence TypeScript is JavaScript but more advanced. Now, CoffeeScript doesn't actually bring that to the table. CoffeeScript syntax is different than JavaScript and requires a different view point to be best understood. For developers with a strong base in JavaScript looking to advance in their web development skills TypeScript is usually their first pick.

So, should I use TypeScript?

If you want sure, but code is meant to be readable for time efficiency purposes and TypeScript doesn't meet that goal, in fact it's even less readable than Vanilla JS in my opinion.

So... CoffeeScript huh?

Are you going to go get it or what?

Go get 'em

Discussion (4)

Collapse
aminnairi profile image
Amin

What sold me was the way top-level await is handled. Deno has this. Stil missing in Node.js and the browser and it saddens me a lot.

endpoint =
  "https://jsonplaceholder.typicode.com/users"

options =
  method: "GET"
  mode: "cors"
  credentials: "include"
  headers:
    "Content-Type": "application/json"
    "Accept": "application/json"

do ->
  try
    response = await fetch endpoint, options
    users = await response.json()
    console.log users

  catch error
    console.error error.message
Enter fullscreen mode Exit fullscreen mode
Collapse
eruizdechavez profile image
Erick Ruiz de Chavez

Just so you know, that is very old (most likely automatically generated) JavaScript.

With new JavaScript some of those expressions can be way shorter and/or better to understand.

// Functions:
square = x => x * x;

// Objects:
math = {
  root: Math.sqrt,
  square, // Short hand object assignment
  cube: x => x * square(x), // Arrow functions
};

// Splats:
race = (winner, ...runners) => print(winner, runners);

// Array comprehensions:
cubes = list.map(item => math.cube(item))
Collapse
seanolad profile image
Sean Author

True, I use modern JS conventions and still CoffeeScript manages to be more concise. Simply because it borrows the simple syntax from ruby. But it's good that you pointed out that the example code I used was based off some pre ES6 principles.

Collapse
eimfach profile image
Robin Gruenke

Actually, when I think of it, CoffeeScript is extremely clean, approachable and expressive, there is nothing quite like it regarding these terms in the JS world except Elm or maybe PureScript (But those are not "it is just Javascript") ... I think I will rewrite my websites vanilla JS to CS ... perfect use case

Forem Open with the Forem app