Search code examples
javascriptswitch-statementternary-operatorternary

Can a JavaScript ternary operator support 3 conditions?


Given the following JavaScript ternary operator, is it possible to enable this to support 3 conditions versus the current two?

const color = d.y >= 70 ? "green" : "red";

I would essentially like the following logic:

>= 70, color = green;
between 69-50, color = yellow;
< 50, color = red;

Is this possible with a 1 line ternary or do I need a IF statement?


Solution

  • you can do

    const color = d.y >= 70 ? "green" : (d.y < 50 ? "red" : "yellow");