328
votes

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
7
<rant> I wish coffee script could have just supported the ternary operator syntax, it's shorter and easier to read than if else then </rant>AJP
@AJP I think the ternary would make coffee less Ruby-ish, even though Ruby has that. The goal with coffee is always readability and rounding off rough corners.jcollum
@jcollum agreed, but what really I find most unsettling is that a = true ? 5 : 10 is valid coffeescript, but does not mean a ternary structure, instead (in javascript) it means: a = true ? true : {5:10} which is known as a bad thing® Additionally a = false ? {5 : 10} in coffeescript then (in javascript) is equivalent to: a = true ? false : {5:10} For what it's worth, I don't think it's good.AJP
It may be for the best to spell out if..then..else for CoffeeScript. The ? as an existence operator makes a lot of sense: beast = yeti ? "bear" or if yeti? then alert "It's a yeti!" makes use the ? quite well.Paul Oliver
Separate ternary operator is not really necessary in CoffeeScript as if/then/else is already an expression and does the same thing. If you're really missing it, then you're rather used to C or JavaScript syntax than really in need of it. If it's not readable enough, and it sometimes does happen, simply wrap whole expression in parentheses. Operator ? has been spared for more useful checks which are absent in JavaScript, as already stated by @PaulOliver. Existential operator is the best.skalee

7 Answers

553
votes

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.

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

See documentation.

22
votes

In almost any language this should work instead:

a = true  && 5 || 10
a = false && 5 || 10
13
votes

Coffeescript doesn't support javascript ternary operator. Here is the reason from the coffeescript author:

I love ternary operators just as much as the next guy (probably a bit more, actually), but the syntax isn't what makes them good -- they're great because they can fit an if/else on a single line as an expression.

Their syntax is just another bit of mystifying magic to memorize, with no analogue to anything else in the language. The result being equal, I'd much rather have if/elses always look the same (and always be compiled into an expression).

So, in CoffeeScript, even multi-line ifs will compile into ternaries when appropriate, as will if statements without an else clause:

if sunny   
  go_outside() 
else   
  read_a_book().

if sunny then go_outside() else read_a_book()

Both become ternaries, both can be used as expressions. It's consistent, and there's no new syntax to learn. So, thanks for the suggestion, but I'm closing this ticket as "wontfix".

Please refer to the github issue: https://github.com/jashkenas/coffeescript/issues/11#issuecomment-97802

3
votes

You may also write it in two statements if it mostly is true use:

a = 5
a = 10 if false

Or use a switch statement if you need more possibilities:

a = switch x
  when true then 5
  when false then 10

With a boolean it may be oversized but i find it very readable.

1
votes

Multiline version (e.g. if you need to add comment after each line):

a = if b # a depends on b
then 5   # b is true 
else 10  # b is false
0
votes

CoffeeScript has no ternary operator. That's what the docs say.

You can still use a syntax like

a = true then 5 else 10

It's way much clearer.