Edit This isn't a question of whether or not the ternary operator is harmful. It's about whether or not there's a consensus about whether or not it should be used outside of assignment statements. /Edit

Based on a boolean, I want to call one of two different functions (with no return value). I wrote this bit of javascript:

(condition) ? doThis(arg) : doThat(arg)

Which I think looks cleaner than writing out an if-else statement:

if(condition) {
else {

My colleagues strongly believe ternary statements should only be used in assignment statements. I can't find style guides that discuss this, probably because languages like Java and C# don't allow this at all (their compilers require ternary operators to return a value). Should this be considered a bad use of the ternary statement? Why?

  • Keep in mind that the Jason Voorhees may be assigned maintain that code of yours. – David Hammen Aug 26 '15 at 2:37
  • 2
    @gnat - not a duplicate of that question, which is about general use of ?: in expressions, whereas this is specifically about using it as a replacement for if-else when there is no single value being calculated. – Jules Aug 26 '15 at 6:43
  • While I'm in two minds as to the appropriateness of the proposed duplicate, this question is still primarily opinion-based, so I don't see any point in reopening it. – Ixrec Aug 27 '15 at 21:15
  • Why do you think this looks cleaner? Breaking conventions like this is opposite of clean. – JacquesB Jan 18 '18 at 17:10

Generally speaking, the conditional operator is intended to make an if statement with a value. w = x ? y : z. Thus, if you're using it for side effects, it's counter-intuitive. Valid, but counter-intuitive; and remember that you're writing code for your teammates to read.

  • This seems right. Just about every usage example I find, including Wikipedia uses the operator to perform conditional assignment. – BobbyA Aug 26 '15 at 17:44

A couple of cases, that are not assignments, where I find ternary operators useful are:

In function arguments: in my opinion

f( e1, .. cond ? em : dm, .. en);

is less error prone than

if ( cond) f( e1, .. em, .. en); else f( e1, .. dm, .. en);

When you want to call one function or another with identical arguments: again I find

(cond ? f : g)( /* long and complex argument list */);

less error prone than

if ( cond) f( /* long and complex argument list */);
else g( /* long and complex argument list */);

As I see it the key thing in each case is that the ? form has less repetition. Repetition opens up the possibility of changing one instance and forgetting to change the other.

  • 5
    In these cases though, you're still using the ternary operator to return a value, which, according to syrion's answer above, is an expected usage. – BobbyA Aug 26 '15 at 17:35
  • @BobbyA what about void functions ? – pllee Aug 26 '15 at 20:54

Here is an example where a non-assignment use comes in handy, specifically to call one of two functions.

In a Javascript Promise, which operates on two function arguments, resolve and reject, it is possible to do the following one-liner without violating the intuition of the ternary operator returning a value:

((this.inputErrors.length === 0) ? resolve : reject)();

To me the above reads cleaner than:

if (this.inputErrors.length === 0) {
} else {

Note: I know this is similar to this answer, but hopefully easier to understand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.