Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shorthand with else if

Tags:

javascript

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
like image 906
Becky Avatar asked Mar 21 '16 04:03

Becky


People also ask

Which is shorthand of if else statement?

The ternary operator, also known as the conditional operator, is used as shorthand for an if...else statement. A ternary operator is written with the syntax of a question mark ( ? ) followed by a colon ( : ), as demonstrated below. In the above statement, the condition is written first, followed by a ? .

What is the shorthand expression for else if in python?

The ternary conditional operator is a short-hand method for writing an if/else statement. There are three components to the ternary operator: the expression/condition, the positive value, and the negative value. When the expression evaluates to true, the positive value is used—otherwise the negative value is used.

How do you write an IF and ELSE condition?

Conditional StatementsUse if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.


1 Answers

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);
like image 125
ChiefTwoPencils Avatar answered Sep 28 '22 04:09

ChiefTwoPencils