DEV Community

Cover image for Better if statement w/ ternary operator ? : 💻

Better if statement w/ ternary operator ? : 💻

thomasaudo on February 05, 2019

The if operator is a mainstay of computer programming, we use it everywhere. Sometimes we need to test a value to then, execute some simple code: ...
Collapse
 
bezo97 profile image
Zotya Docs

If we're really going for simplicity, in this particular case you can just say

adult = (age>=18)

since the A and B operations just give back the result of the condition.
Here's one example that better demonstrates the case:

result = (age>=18 ? "adult" : "young")

Cheers!

Collapse
 
link2twenty profile image
Andrew Bone

You don't need the brackets either 🙂

Collapse
 
alainvanhout profile image
Alain Van Hout • Edited

A great many people mistake a decrease in number of characters for an increase in clarity. Or to say it in another way: dense isn't the same as concise.

Collapse
 
thomasaudo profile image
thomasaudo • Edited

Yes as I said in the conclusion, this syntax isn't adapted in all case, only in really simple one