Search code examples
javascriptnode.jsconditional-operator

Nested conditional ternary operator in javascript


I have the below code, where idName is selected based on id1 or id2.

const getValues = async ({ id1, id2, ...request}) => {
   const id = id1 || id2;
   const idName = id1? 'uid' : 'sid';
 };

Now, I add another parameter "id3" and which needs to get selected when id1 is not passed in as input. But the below code throws an error. Any leads would be appreciated.

const getValues = async ({ id1, id2, id3, ...request}) => {
   const id = id1 || id2 || id3;
   const idName = id1 ? id3 ? ('uid' : 'sid') ? (id3);
 };

Solution

  • const getValues = async ({ id1, id2, id3, ...request}) => {
       const id = id1 || id2 || id3;
       const idName = id1? 'uid': id3? 'something-for-id3': 'sid'
    };