is there anything like (1==0 ? "no" : "yes") in javascript?
facepalm :P
annakata
2009-06-02 08:28:26
easiest 80 rep ever. ;) +1 make it 90.
Paolo Bergantino
2009-06-02 08:39:10
+1_____________Populist badge incoming.
antony.trupe
2009-06-03 04:07:35
+4
A:
Yes - it's the conditional operator, and the syntax is exactly as you've written it.
Jon Skeet
2009-06-02 08:25:45
Thanks John - I think it was a case of hitting 'v' instead of Ctrl-V :)
Jon Skeet
2009-06-02 08:33:24
Jon, I've got no idea what you were (trying) to link to, I've added a link that is relevant.
Unkwntech
2009-06-02 08:33:29
Hmm looks like we edited it at the same time... and it looks like we may have linked to the same place....
Unkwntech
2009-06-02 08:34:44
Looks like it, yes :) Without the link it's a pointless answer, but hopefully with the link it's useful...
Jon Skeet
2009-06-02 08:35:43
Uh oh Jon Skeet answering a Javascript question? Get out of my turf, man! ;)
Paolo Bergantino
2009-06-02 08:38:47
@Paolo lol, I still laugh to myself when I see the one question where my answer came in higher then his.
Unkwntech
2009-06-02 09:26:32
The thing that bothers me is that it's technically the ternary operator, and even more technically, it's not an operator, it's a structure...
Antony Carthy
2009-06-02 09:49:21
@Antony: Unless JavaScript is different (and broken :) its name is the conditional operator and it's *a* ternary operator in the same way that / is *a* binary operator. ECMA-262 certainly calls it the conditional operator (see section 11.12).
Jon Skeet
2009-06-02 10:22:26
(It's a common fallacy in many languages to call the conditional operator "the ternary operator". Why talk about an operator in terms of its arity instead of its purpose? What happens if the language later gains *another* ternary operator?)
Jon Skeet
2009-06-02 10:23:41
+8
A:
JavaScript has the conditional operator, so it's perfectly legitimate to do:
var response = (1 == 0 ? "no" : "yes");
John Topley
2009-06-02 08:27:34
Heh. I was future-proofing it for quantum computing where 1 is both 1 and 0 simultaneously! ;-)
John Topley
2009-06-02 08:34:23