CoffeeScript basics

Let's get started! We'll begin with something simple:

x = 1 + 1

You can probably guess what JavaScript this will compile to:

var x;
x = 1 + 1;

Statements

One of the very first things you will notice about CoffeeScript is that there are no semicolons. Statements are ended by a new line. The parser usually knows if a statement should be continued on the next line. You can explicitly tell it to continue to the next line by using a backslash at the end of the first line:

x = 1\
  + 1

It's also possible to stretch function calls across multiple lines, as is common in "fluent" JavaScript interfaces:

"foo"
  .concat("barbaz")
  .replace("foobar", "fubar")

You may occasionally wish to place more than one statement on a single line (for purely stylistic ...

Get CoffeeScript Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.