I personally love ternary operators, and in my humble opinion, they make complicated expressions very easy to digest. Take this one:
word = (res.distance === 0) ? 'a'
: (res.distance === 1 && res.difference > 3) ? 'b'
: (res.distance === 2 && res.difference > 5 && String(res.key).length > 5) ? 'c'
: 'd';
However in our project's ESLINT rules nested ternary operators are forbidden, so I have to get rid of the above.
I'm trying to find out alternatives to this approach. I really don't want to turn it into a huge if / else statement, but don't know if there's any other options.
If you are looking to use const with a nested ternary expression, you can replace the ternary with a function expression.
or using named parameters via destructuring...
... edit: It may be clearer to model it after the python if expression like this:
We can simplify it using basic operators like && and ||
You could write an immediately invoked function expression to make it a little more readable:
Link to repl