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

2 Answers 2

1
con ? (con2 ? result1 : result2) : null;
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, tell me please, could i use it without ":null" ?
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.
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?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.