O'Reilly logo

The Little Book on CoffeeScript by Alex MacCaw

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 6. The Little Conclusion

By Jeremy Ashkenas[1]

You’ve reached the end of The Little Book on CoffeeScript, and now you know just about everything you’ll need. CoffeeScript is a little language at heart, and if you know JavaScript and have a sense of the philosophy behind the changes that CoffeeScript makes to JavaScript, you should be able to get up and running very quickly indeed.

Philosophy

Unlike most programming languages, CoffeeScript was never designed from the ground up. It has always been an attempt to express core JavaScript concepts in as simple and minimal a syntax as we can find for them.

Let’s take a simple function. To produce the square of x, we multiply x by itself. In JavaScript:

var square = function(x) {
  return x * x;
};

To derive the CoffeeScript for this, let’s think through the steps it would take to reduce this function to its essential features.

  • We can understand the code perfectly well without the semicolons, so let’s drop those.

  • Instead of using { and } to delimit the body of the function, let’s use the indentation that’s already present on the page.

  • It’s clear that we’re defining a new function, so let’s drop the redundant var in front of the assignment.

  • Every construct in CoffeeScript should be an expression with a value, so the natural value of a function body is the last line of code it executes ... allowing us to omit the return.

  • Finally, we replace function( input ){ output } with a function literal that visually represents the idea that the input of ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required