O'Reilly logo

CoffeeScript Programming with jQuery, Rails, and Node.js by Michael Erasmus

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

Conditional clauses and logical aliases

CoffeeScript introduces some very nice logic and conditional features, some also borrowed from other scripting languages. The unless keyword is the inverse of the if keyword; if and unless can take the postfix form, meaning statements can go at the end of the line.

CoffeeScript also provides plain English aliases for some of the logical operators. They are as follows:

  • is for ==
  • isnt for !=
  • not for !
  • and for &&
  • or for ||
  • true can also be yes, or on
  • false can be no or off

Putting all this together, let's look at some code to demonstrate it:

CoffeeScript:

car.switchOff() if car.ignition is on
service(car) unless car.lastService() > 15000
wash(car) if car.isDirty()
chargeFee(car.owner) if car.make isnt "Toyota"

JavaScript: ...

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