Journal entry

Geekpulp

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: [code] if (bananaIsCool) { eatWith(‘spoon’); } else { eatWith(‘scissors’); } [/code]

Simple enough, but using a ternary we can do even better: [code] bananaIsCool ? eatWith(‘spoon’) : eatWith(‘scissors’); [/code]

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.