78

I really like this:

var value = maxValue > minValue ? minValue : maxValue;

Is there something equally concise in Coffescript?

Blub
  • 13,014
  • 18
  • 75
  • 102
  • 8
    Thank you very much for not calling it "the ternary operator". – Mark Wilden Apr 10 '12 at 19:24
  • 4
    @MarkWilden why shouldn't he? it's an established term – Stephan Sep 10 '14 at 19:48
  • 1
    If you look for ternary operator for coffescript you find it right away. – william.eyidi Jan 14 '15 at 16:22
  • @MarkWilden: You got 5 upvotes, so it seems quite a few people agree with you, but would you please explain why? – Zaz May 21 '15 at 00:35
  • @Zaz `conditional operator` is easier to be remembered than `ternary operator` – Hai Feng Kao Sep 15 '15 at 09:52
  • Because frontend devs aren't expected to have a CS background, so terms like 'ternary' are foreign? – weberc2 Oct 27 '15 at 20:25
  • 3
    "The trouble with 'the ternary operator' is that it describes what it *looks like,* not what it *does.*" -- Eric Lippert, http://blogs.msdn.com/b/ericlippert/archive/2010/02/18/whats-the-difference-between-ternary-and-tertiary.aspx (in the comments) – Joe White Nov 29 '15 at 03:53

7 Answers7

147
value = if maxValue > minValue then minValue else maxValue
20

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

value = Math.min(minValue, maxValue)
Ricardo Tomasi
  • 34,573
  • 2
  • 55
  • 66
  • 12
    in coffee, `value = Math.min minValue, maxValue` ;) – v42 Nov 18 '11 at 17:18
  • 5
    I don't think the question was referring to the actual content, but to the ability to do it all on one line. Could be wrong though. – LasagnaAndroid Oct 14 '14 at 07:10
  • 2
    Wrong as it does not addresses the question. The OP asks about `Conditional operator in Coffeescript` and how to write it in, giving the `JS` example. It is just an example and has nothing to do with the actual syntax problem. – jimasun Aug 10 '16 at 11:58
12

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

value = `maxValue > minValue ? minValue : maxValue`
Peter Krnjevic
  • 1,070
  • 15
  • 20
10

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.

Brian Genisio
  • 47,787
  • 16
  • 124
  • 167
  • 20
    There's no way to have the `a ? b : c` ternary in CoffeeScript without ambiguity, since `a ? b` has a meaning ("`a` if it's non-null, `b` otherwise), and `b: c` has one as well (`{b: c}`). – Trevor Burnham Nov 17 '11 at 17:20
3

You can write it like this:

value = if maxValue > minValue then minValue else maxValue

It will compile like your code.

v42
  • 1,415
  • 14
  • 23
3

Below is the fact:

In the documentation, there's a section titled "Conditionals, Ternaries, and Conditional Assignment". This leads one to believe that coffeescript supports

condition ? when-true : when-false 

but in fact it does not.

Below is the information about the patch which will solve this issue

Here's the patch (and it's pushed to coffeescript.org):

http://github.com/jashkenas/coffee-script/commit/ec2d358ae3c82e9888c60695d7cce05edde0c55a

Examples:

mood = greatlyImproved if singing

if happy and knowsIt
  clapsHands()
  chaChaCha()
else
  showIt()

date = if friday then sue else jill

options or= defaults
Siva Charan
  • 17,940
  • 9
  • 60
  • 95
  • 2
    `if then else` **is** a ternary operation, it just has different syntax. – Ricardo Tomasi Nov 18 '11 at 00:58
  • in coffee script, if else statement is written in one line statement. – Siva Charan Nov 18 '11 at 07:09
  • 1
    what i meant is that a ternary operation is defined as *an operation with three elements*. A one-liner `if then else` statement classifies as *ternary*. The correct term for the "ternary" operator in javascript is **conditional operator** anyway. – Ricardo Tomasi Nov 18 '11 at 09:50
-1
value = maxValue > minValue && minValue || maxValue

This is actually not correct, check the comments.

Răzvan Flavius Panda
  • 21,730
  • 17
  • 111
  • 169
Sergey Semenov
  • 369
  • 2
  • 7
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – PlasmaHH May 28 '14 at 15:25
  • @PlasmaHH: This is a valid answer actually, only a little less readable. – Răzvan Flavius Panda Jun 06 '14 at 16:00
  • 1
    @RăzvanPanda No, it is not a valid answer, because it is doing slightly different thing than requested and the difference is not described/documented in the answer. Eg. if `minValue` equals `0` and `maxValue` equals `5`, then `maxValue > minValue ? minValue : maxValue` would return `0`, while `maxValue > minValue && minValue || maxValue` would return `5`. – pepkin88 Feb 08 '15 at 13:35
  • @pepkin88: That is correct, this answer does not work when being passed a falsy value for minValue and the condition is true. – Răzvan Flavius Panda Feb 08 '15 at 13:55