DEV Community

The Ternary Operator vs The Conditional Operator WTF

JavaScript Joel on August 31, 2018

Almost a year ago I received a comment on one of my articles. Its name isn’t “the ternary operator”. It IS a ternary operator The argument go...
Collapse
 
antogarand profile image
Antony Garand

Tldr:

  • Binary operator = operator with two variables, such as 1 + 2_
  • Ternary operator = operator with three variables, such as true ? 1 : 2

The conditional operator (?:) is a ternary operator, and the addition operator (+) is a binary operator.

As the conditional operator is the only ternary operator, people usually refer to it as the ternary operator, but that isn't completely true.

Collapse
 
joelnet profile image
JavaScript Joel

Correct (mostly).

While The Conditional Operator's name is not The Ternary Operator, it is however correct to reference it by it's type as The Ternary Operator. The difference being one you are calling by name and one by type.

Collapse
 
bluebell_lester profile image
Bluebell Lester

So in the end it doesn't matter what you'd call it until someone comes up with another ternary operator.

Maybe you should call it the ternary conditional operator :D

Collapse
 
danieljsummers profile image
Daniel J. Summers

console.write(post.author === 'joelnet' ? 'You are correct' : 'BZZZZZT!')

>You are correct

I could see the verbiage "a ternary operator" if you're trying to get across that you're not limited to one per statement / function / module / etc., but until they define another one, ? is the ternary operator.

Collapse
 
joelnet profile image
JavaScript Joel

Now I am curious about what a second ternary operator would do...

Collapse
 
danieljsummers profile image
Daniel J. Summers

It does seem that you really only need one, as others could be expressed in terms of the first. I actually gave this probably more thought than I should have, but the only things I could come up with were not conditional operators; they were more combinations of prefix/postfix/infix operators.

Collapse
 
ederchrono profile image
Eder Díaz

I'm guilty of using 'a ternary operator' more than once.
In my defense it happens because when we have a couple of appearances of the ternary operator in an algorithm and then I want the other person to add another, I think it is more practical to say "add a ternary over there" instead of "add the ternary operator" because it could happen that the other person things I'm referring to a previous one.

Collapse
 
joelnet profile image
JavaScript Joel

I think that is correct English when used this way. Because you could also say "add a conditional operator over there".

Maybe someone with an English degree can explaining these rules :)

Collapse
 
andrewlucker profile image
Andrew Lucker • Edited

Ternary,+ operators are syntax sugar. The if-expression is the only one common enough to be present in most languages though.

In python: 1 < 2 < 3 or 1 < 2 < 3 < 4 expands to 1 < 2 and 2 < 3 ...

In haskell you can sort of write your own operators with n-ary syntax.

Macros also can provide n-ary syntax. React would be an example of this in Javascript+jsx.

I've got your back

Collapse
 
bgadrian profile image
Adrian B.G.

My OCD brain wouldn't let this die and I kept thinking about that argument.

And now you triggered 100 more OCD brains, damn you.

Collapse
 
joelnet profile image
JavaScript Joel

you should know full well that it couldn't be helped :D

Collapse
 
moe64 profile image
Moe

You brung us back to school 🏫 with this one! 😂 you gonna learn today gif

Collapse
 
joelnet profile image
JavaScript Joel

HA HA HA

Collapse
 
dinsmoredesign profile image
Derek D

I've always called it a "ternary expression" 😭

Collapse
 
alainvanhout profile image
Alain Van Hout • Edited

I tend to refer to the usage of that operator as ‘an inline conditional’. But as long as everybody understands (i.e. there is little or no room for misunderstandings) it doesn’t matter that much.

Collapse
 
joelnet profile image
JavaScript Joel

it doesn’t matter that much.

That's the truth!