CoffeeScript - A Fun Little Language

11 Jan 2010 | By Ric Roberts | Tags libraries

Jeremy Ashkenas has just released version 0.2.3 of his experimental CoffeeScript language. Jeremy describes it as “JavaScript’s less ostentatious kid brother — the same genes, roughly the same height, but a different sense of style”.

Most statements in CoffeeScript have their analogues in Javascript: CoffeeScript just provides an alternative way to express them. For me, CoffeeScript brings to mind certain aspects of JSON, Ruby, and maybe Python too.

CoffeeScript is actually a Ruby Library (available as a gem) that provides a coffee command that can compile .coffee files into Javascript.

# Assignment:
number: 42
opposite_day: true

# Conditions:
number: -42 if opposite_day

# Functions:
square: x => x * x

# Arrays:
list: [1, 2, 3, 4, 5]

# Objects:
math: {
  root:   Math.sqrt
  square: square
  cube:   x => x * square(x)
}

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

# Existence:
alert("I knew it!") if elvis?

# Array comprehensions:
cubed_list: math.cube(num) for num in list

There’s some thorough documentation on Github, including loads of little runnable snippets. Jeremy has also produced a full port of the Underscore JavaScript library (that we mentioned a couple of months ago), which serves as a good example.


blog comments powered by Disqus