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 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);
The syntax would be:
var icon = (area == 1) ? icon1 : (area == 2) ? icon2 : icon0;
But this is starting to get complicated. You may well be better off just creating a function to do this work instead:
var icon = getIcon(area);
function getIcon(area) {
if (area == 1) {
return icon1;
} else if (area == 2) {
return icon2;
}
return icon0;
}
For anyone that's confused about the multiple ternary syntax (like I was), it goes like this:
var yourVar = condition1 ? someValue
: condition2 ? anotherValue
: defaultValue;
You can add as many conditions as you like.
You can read up further on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator