Becky Becky - 5 months ago 9
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
if
and
else
. But How do I write this when there's an
else if
statement?

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

Answer

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);