0

How to convert if/else to ternary if/else?

if(con){
   if(con2){
     result1
   }else{
     result2
   }
}

i tried , but i get miss.. :

con?con2?result1:result2;
Kimi
  • 23
  • 3

2 Answers2

1
con ? (con2 ? result1 : result2) : null;
C.Gibby
  • 115
  • 1
  • 7
  • thanks, tell me please, could i use it without ":null" ? – Kimi Mar 20 '19 at 13:47
  • A ternary has to have an "else" option. If there's a value that is more appropriate than null you can use it instead. In my opinion the if else statement you posted in the original question seems to be the more appropriate solution to what you are trying to achieve here. – C.Gibby Mar 20 '19 at 13:52
0
 con && (con2 ? result1 : result2)

You can use && if you don't have an else branch. But why would you replace a beautiful if statement with such unreadable shortforms?

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151