Meteor and CoffeeScript

Philipp Muens
2 min readOct 28, 2015

--

You may have tinkered around with ES6 / ES2015 which is enabled by default in Meteor 1.2.
ES6 introduces some new syntax features such as arrow functions which enables an easier and more comprehensive way to write modern JavaScript.

With Meteor you a not forced to write your application in JavaScript. Thanks to the package system you can add e.g. CoffeeScript which is a nice alternative to old fashioned JavaScript.

In this blog post we’ll take a closer look how you can use CoffeeScript in your Meteor application.

What is CoffeeScript?

Do you want to write some easier JavaScript code with less braces and semicolons (which results in ~30% less code)?
Then CoffeeScript might just be what you’re looking for.

CoffeeScript is a programming language which transcompiles into JavaScript.
You write some fancy CoffeeScript code and after a compilation-Step you’ll get the equivalent JavaScript code.

The important thing to notice is that “CoffeeScript is just JavaScript” which means that all your written CoffeeScript-Code will result in valid JavaScript-Code and vice versa.

Comparing CoffeeScript to JavaScript

Here are some examples (inspired by the great examples on the CoffeeScript homepage) so you can see what this means in practice:

# CoffeeScript<br />
return true if num is 12</p>
<p># Equivalent JavaScript<br />
if (num === 12) {<br />
return true;<br />
}<br />
# CoffeeScript<br />
return true if num is 12</p>
<p># Equivalent JavaScript<br />
if (num === 12) {<br />
return true;<br />
}<br />
# CoffeeScript<br />
greet person for person in ['John Doe', 'Alexa Alvin', 'Michael Michigan']</p>
<p># Equivalent JavaScript<br />
var person, _i, _len, _ref;<br />
_ref = ['John Doe', 'Alexa Alvin', 'Michael Michigan'];<br />
for (_i = 0, _len = _ref.length; _i < _len; _i++) {<br />
person = _ref[_i];<br />
greet(person);<br />
}<br />

Benefits using CoffeeScript

As you may see CoffeeScript is great because it automatically applies best practices for your JavaScript code (e.g. checking for equality with === instead of ==) which yields into better JavaScript-Code and fixes some downsides of JavaScript.

“Wait, What? I need to run a compilation step?”

Yes, all your CoffeeScript must be compiled but the good thing is that Meteor automatically compiles all your CoffeeScript-Code for you.

Using CoffeeScript with Meteor

The following command will add CoffeeScript to your Meteor-Project:

meteor add coffeescript<br />

By the way: The CoffeeScript-Package is officially maintained by the Meter development group.

The only thing you need to do is to write the code and hit save. That’s it.

Meteor will grab all your .coffee-Files and compile them into JavaScript behind the scenes.

Pro tip: js2coffee is a great tool which helps you while migrating you JavaScript-Files to CoffeeScript.

I know that there are different opinions about CoffeeScript vs. JavaScript. Just give CoffeeScript a shot and see for yourself if it satisfies your needs.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Philipp Muens
Philipp Muens

Written by Philipp Muens

👨‍💻 Maker — 👨‍🏫 Lifelong learner — Co-creator of the Serverless Framework — https://philippmuens.com

No responses yet