Writing a shorthand JS if/else statement

Lets start today's lesson with something short and sweet.

We're going to talk about Conditional (ternary) Operators. What? Wait a minute? I know, I didn't know that either, but thanks to Matt Deloughry I do now.

You can find out more about them on the MDN.

So here goes…

Probably the most common thing after a function that's written everyday in JS is an if/else statement and if like me you think "There's got to be a better way," I'm here to tell you the good news. There is.

Instead of writing:

var variable = valueIfFalse;  
if (condition) { variable = valueIfTrue }  

You can trim it down to:

var variable = condition ? valueIfTrue : valueIfFalse;  

And that's it.