+ 2
Ternary operators with disjunction?
Is it possible to add disjunction in condition? I want to add an empty string besides null. https://code.sololearn.com/WC7S8dPpgi3Q/?ref=app
3 Answers
+ 12
Yes it's entirely possible to combine multiple conditions with conditional operator.
However in this case only 1 condition is suffice to test for null or empty string as both evaluates to falsy in JavaScript.
Hence,
name ? name : 'Anonymous'
P/S: When in doubt next time, you may experiment it by yourself. đ
+ 4
For multiple disjunction, check out this challenge I did:
To calculate interest based on different amount input range:
function interest(a) {
return a>90000? a*0.05:
a>50000? a*0.04:
a>20000? a*0.03:
a>5000? a*0.02:0;
}
https://code.sololearn.com/W6qZPtRnIFYt/?ref=app
0
how to write single ternary operator using three variables in