DEV Community

Discussion on: Ternary Operators In JavaScript

Collapse
 
thomasjunkos profile image
Thomas Junkツ • Edited

Ternaries as shown here are mostly fine.

I use them mostly to provide variables with (default) values which come in a binary flavor.

For functions I prefer


chrisPratt = (marvel)=>{
if(marvel) return "I am Star-Lord!";
return "Have you watched Guardians of the Galaxy?";
};

because your eyes rest on the condition and it is easy from that to look at the non matching case.

But I admit, this has more to do with personal taste ;)

You should add a warning sign not to overuse ternaries, esp. start nesting those guys. This could end up pretty nasty.

Collapse
 
iandavid profile image
David Okpare

Thanks for highlighting it. I have included it in the post.