tags:

views:

234

answers:

3

is there anything like (1==0 ? "no" : "yes") in javascript?

+18  A: 

yes, exactly as you wrote it :)

Philippe Leybaert
facepalm :P
annakata
easiest 80 rep ever. ;) +1 make it 90.
Paolo Bergantino
+1_____________Populist badge incoming.
antony.trupe
+4  A: 

Yes - it's the conditional operator, and the syntax is exactly as you've written it.

Jon Skeet
Might want to check that link, Jon.
John Topley
Thanks John - I think it was a case of hitting 'v' instead of Ctrl-V :)
Jon Skeet
Jon, I've got no idea what you were (trying) to link to, I've added a link that is relevant.
Unkwntech
Hmm looks like we edited it at the same time... and it looks like we may have linked to the same place....
Unkwntech
Looks like it, yes :) Without the link it's a pointless answer, but hopefully with the link it's useful...
Jon Skeet
Uh oh Jon Skeet answering a Javascript question? Get out of my turf, man! ;)
Paolo Bergantino
@Paolo lol, I still laugh to myself when I see the one question where my answer came in higher then his.
Unkwntech
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
@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
(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
+8  A: 

JavaScript has the conditional operator, so it's perfectly legitimate to do:

var response = (1 == 0 ? "no" : "yes");
John Topley
I'd refactor that to var response = "yes"; // :)
Philippe Leybaert
Heh. I was future-proofing it for quantum computing where 1 is both 1 and 0 simultaneously! ;-)
John Topley
Then you should've refactored it to var response = "yesno";
Philippe Leybaert