Ternary operation in CoffeeScript

evfwcqcg picture evfwcqcg · Apr 13, 2012 · Viewed 100.7k times · Source

I need to set value to a that depends on a condition.

What is the shortest way to do this with CoffeeScript?

E.g. this is how I'd do it in JavaScript:

a = true  ? 5 : 10  # => a = 5
a = false ? 5 : 10  # => a = 10

Answer

loganfsmyth picture loganfsmyth · Apr 13, 2012

Since everything is an expression, and thus results in a value, you can just use if/else.

a = if true then 5 else 10
a = if false then 5 else 10

You can see more about expression examples here.