Search code examples
javascriptreactjsconditional-operator

JS Nested Ternary with Multiple conditions


Essentially I have the following issue:

if condition A & B are true ->. do thing A
if only condition A is true -> do thing B 
else -> do thing C

I tried this :

const myThing = conditionA ? conditionB ? thingA :
conditionA ? thingB : thingC;

It doesn't like the syntax but I am not sure what is wrong with it.


Solution

  •     const myThing = (conditionA && conditionB) ? thingA : (conditionA) ? thingB : thingC;
           
    

    the same as:

    if(conditionA && conditionB){
       thingA
    }
    else if(conditionA){
       thingB
    } else {
       thingC
    }