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 ...

Get CoffeeScript, 2nd Edition 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.