DEV Community

JavaScript Joel
JavaScript Joel

Posted on • Edited on

The Ternary Operator vs The Conditional Operator WTF

Three Monkeys

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 goes on to explain because it is A Ternary Operator, I should refer to it as The Conditional Operator.

This disagreement about language isn't limited to just this one comment thread. I have even seen people correcting others to use A Ternary Operator on top answers on Stack Overflow.

Often called "the ternary operator," but in fact it's just a ternary operator [an operator accepting three operands]. It's the only one JavaScript currently has, though.

Spelling, Grammar and English in general aren't my strong subjects (I do better with 0's and 1's). But, even if I was using the English language incorrectly, I didn't feel like it took anything away from the article. So I had dismissed this as being trivial.

After a fairly lengthy discussion about the grammar. I thought I had made some persuasive arguments. I even referenced some usages of "the ternary operator" in the Mozilla docs. Even so, neither of us were able to come to agreement.

So why am I still thinking about this almost a year later?

My OCD brain wouldn't let this die and I kept thinking about that argument. The other side of the argument was logical. It made sense to me. I understood it. But still, for some unknown reason, I didn't like it. To me it was still The Ternary Operator.

This unresolved discussion left much dissatisfaction in my mind for a number of reasons. First, only one of us can be correct, which means one of us has to be wrong. Second, the correct person (even though correct) has also failed because of their inability to effectively communicate their ideas to the other. Dissatisfaction for both parties.

The Ternary Operator

So here is my argument to persuade everyone once and for all that the correct usage is either The Ternary Operator or The Conditional Operator and it is incorrect to say A Ternary Operator.

First we have to lay out a foundation of facts that we can all agree upon. Without this foundation, it will not be possible to come to an agreement.

Fact 1: JavaScript has a Conditional Operator that is of type Ternary Operator. If there was a Venn diagram, the Conditional Operator would be inside of the Ternary Operator.

Fact 2: JavaScript has exactly one Ternary Operator. In the Venn diagram, The Ternary Operator circle would contain no circles, other than The Conditional Operator circle.

Fact 3: When using A/AN you are referring to one of a collection of items. Examples could be "A Shirt" or "A Dollar".

Fact 4: When using THE you are referring to a specific item and if there is a collection, a specific item of that collection is known and could not be confused for another item. For example there could only be 1 in that collection. Examples could be "The Cat" or "The Car".

So if we can agree on these facts, then it should be clear that THE is the correct usage for The Ternary Operator because JavaScript contains exactly one Ternary Operator.

If JavaScript contained more than one Ternary Operator, then and only then could The Conditional Operator be referred to as A Ternary Operator.

Examples

My house contains only one cat, his name is Mojo. Mojo is of type Cat, like how The Conditional Operator is of type Ternary Operator. So I can also demand "Bring me THE cat" and you will understand. If I had spoke "Bring me A cat", this would cause confusion and you might think I am asking for a cat other than Mojo.

If you are helping me cook, I can say "Get me A container" of which there is more than one to choose from. You pick a Mixing Bowl which is of type Container. But once you place that bowl on the counter I can say "Put the brussel sprouts in THE container" because there is a specific container I am referring to. If there were no containers on the counter, it would be incorrect for me to say "Put the brussel sprouts in THE container" because there are more than one.

"Pass me THE remote". I have multiple remotes in my house, but you damn well know which one I am referring to. So even though there are multiple remotes, a specific remote is in context, allowing me to use THE.

/rant

tl;dr It is correct to refer to JavaScript's Conditional Operator as The Ternary Operator. It is incorrect to use A Ternary Operator a language that doesn't have a singular Ternary Operator.

I have done my absolute best job to put and end to this discussion. Not for your benefit. See I don't care about you at all, I just want to get this dammed loop out of my brain ARG!!! The only way to do so was to write this f'n article.

This was wordy, sure. I know all too well that I have to go into as much detail as humanly possible. That is because I fully expect one of you poindexters (in your best Jerry Lewis voice) to come back with some "Well, in the instance of multiple species of fish it is actually correct to use the plural form Fishes instead of Fish." But please don't. Give my poor little wee brain a break! lol

I do hope you got some enjoyment from my insane ramblings.

Does your brain get hung up on trivial nonsense like this? I would love love love to hear some stories!

Read more of my functional programming-ish articles.

Oh and follow me on the tweety @joelnet

Cheers!

Top comments (16)

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!