javascript
javascript copied to clipboard
adding ternary operator along with example
@hshoff any opinions here?
Let's just add an extra bullet to Conditional Operators & Equality under Use shortcuts. that says For readability, don't nest ternary operators.
(i realize there are fancier ways to write fizzbuzz)
// bad
for (var i = 1; i <= 16; i++) {
var msg = i % 3 === 0 ? i % 5 === 0 ? 'FizzBuzz' : 'Fizz' : i % 5 === 0 ? 'Buzz' : '';
console.log(msg || i);
}
// good
for (var i = 1; i <= 16; i++) {
var msg = '';
if (i % 3 === 0) {
if (i % 5 === 0 {
msg = 'FizzBuzz';
} else {
msg = 'Fizz';
}
} else if (i % 5 === 0) {
msg = 'Buzz'
}
console.log(msg || i);
}
// good
var superpower = isSuperman(user) ? 'flight' : 'none';
Hello there,
This cases should be resolved too :
var y = a ?
longButSimpleOperandB : longButSimpleOperandC;
var z = a ?
moreComplicatedB :
moreComplicatedC;
or
var z = a
? moreComplicatedB
: moreComplicatedC;
Related to #673.