Skip to content

CoffeeScript Cheatsheet

Little language that compiles one-to-one into JavaScript.

01

Getting Started

Hello World

Parens are optional for function calls.

coffeescript
console.log "Hello, World!"

Variables

No var/let/const — variables are auto-scoped.

coffeescript
x = 10
y = 20
console.log x + y

Functions

-> defines functions; last expression is returned.

coffeescript
add = (a, b) -> a + b
console.log add 3, 4

square = (n) -> n * n

Fat Arrow

=> binds 'this' to the defining scope.

coffeescript
this.handler = =>
  console.log this
02

Features

Comprehensions

List comprehensions like Python.

coffeescript
squares = (n * n for n in [1..5])
evens = (n for n in [1..10] when n % 2 == 0)

Destructuring

Destructuring for objects and arrays.

coffeescript
{name, age} = person
[first, rest...] = array

Classes

Class syntax with @ for this.

coffeescript
class Animal
  constructor: (@name) ->
  speak: -> console.log @name

class Dog extends Animal
  speak: -> console.log "#{@name} says woof"

Existential

? checks for existence; ?. for safe chaining.

coffeescript
if name? then greet name
result = obj?.property?.method?()

Was this helpful?