O'Reilly logo

CoffeeScript, 2nd Edition by Trevor Burnham

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

Functions 101

Fire up your favorite text editor and create a new coffee file, because it’s time to call our first function:

 console.log ​'Hello, functions!'

Hit your editor’s Run command, and you’ll get this greeting:

 Hello, functions!

No surprises there. The only CoffeeScript-specific feature we’re taking advantage of is implicit parentheses, which allows us to pass arguments to console.log without wrapping them in ().

Now let’s make things a little more interesting:

 console.log (-> ​'Hello, IIFE!'​)()
 Hello, IIFE!

What happened? Let’s break it down:

  1. -> defines a function. CoffeeScript uses these two characters in lieu of JavaScript’s function keyword.

  2. The function returns the string ’Hello, IIFE!’ because CoffeeScript functions have ...

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