Conditional operator in Coffeescript

value = if maxValue > minValue then minValue else maxValue

There is a more concise option in both javascript and coffeescript :)

value = Math.min(minValue, maxValue)

As Răzvan Panda points out, my comment may actually one of the better answers:

value = `maxValue > minValue ? minValue : maxValue`

This is a case where it feels like CoffeeScript has competing philosophies:

  1. Be concise
  2. Don't be redundant

Since all operations return a result, the if/then/else way of doing things gives you what you need. Adding the ?/: operator is redundant.

This is where I wish they'd give us the ?/: ternary operator even though it is redundant... it simply reads better than the if/then/else variant.

Just my 2c.


You can write it like this:

value = if maxValue > minValue then minValue else maxValue

It will compile like your code.