If you find yourself with a conditional in the form on an if else, there’s a more concise way of writing it. Say hello to a ternary. Take this code:
if (bananaIsCool) {
eatWith('spoon');
} else {
eatWith('scissors');
}
Simple enough, but using a ternary we can do even better:
bananaIsCool ? eatWith('spoon') : eatWith('scissors');
So how’s this thing structured? Basically, we start with the item we are checking for truthiness, bananaIsCool, followed by a ? and what we want to happen if the item is truthy, eatWith('spoon'). That’s then followed by a : and what we want to happen if the items falsey, eatWith('scissors').
More concise, still readable. Nice!
Need more details? Check out MDN.
