Becky Becky - 1 year ago 50
Javascript Question

Shorthand with else if

How do I write shorthand for else if statements?

if (showvar == "instock"){
//show available
} else if (showvar == "final3"){
//show only 3 available
} else {
//show Not available

I know to write when there's only
. But How do I write this when there's an
else if

(showvar == "instock")? //show available : //show Not available

Answer Source

You simply nest the else ifs on the false side of :; else clauses are simply false as well. Like so...

(showvar == "instock") ? 
show available : ((showvar == "final3") ? 
show only 3 available : show Not available);