Skip to content
Advertisement

Convert if statements into ternary conditional operators

Apologize if it’s seems simple, I’m stack on this. I want to convert these if statements into ternary condition :

JavaScript

I tried this and do not work for me, it only execute directly condition C

JavaScript

How to formulate that ? thanks.

Advertisement

Answer

Though with some magic letters it works. But please DON’T do it.

Nested terenary operations are harder to read and makes your Juniors cry. Don’t do it.

If you still want to do

JavaScript

Example

JavaScript

Example 2 :

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement