JS Ternary functions with multiple conditions?
Yes you can go wild nesting ternaries. I find this version to be fairly readable:
var foo = ( bar === 'a' ? 1 : // if bar === 'b' ? 2 : // else if bar === 'c' ? 3 : // else if null // else );
but that's not a widely shared opinion, and you should probably stick to if/else
or switch
when working on a team.
Yes, you can use multiple condition in Ternary Operator. Hope this will help you.
var x=20;var y = x<13 ? "Child" : x<20 ? "Teenage" : x<30 ? "Twenties" : "Old people";console.log(y);
A switch statement is likely the best choice in a situation like this.
let inputOneAns;switch(inputOne) { case "Yes": inputOneAns = "517"; break; case "No": inputOneNas = "518"; break; default: inputOneNas = "";}
If you could do ternary operations beyond 2 conditions, they would become incredibly messy. You can put conditions together, but I've no idea why you would want that - that would be incredibly messy.