I have two variables foo
& bar
and I want to declare them based on a condition
.
I have already shortened the code from this:
let foo = '';
let bar = '';
if (condition) {
foo = 'hi';
bar = 'bye';
} else {
foo = 'bye';
bar = 'hi';
}
To this:
const foo = condition ? 'hi' : 'bye';
const bar = !condition ? 'hi' : 'bye';
I still feel the code is repetitive with having to use ternary operator twice. Is there anyway I can shorten this code more? Thanks :)
You could take a destructuring with a single condition.
let [foo, bar] = condition ? ['hi', 'bye'] : ['bye', 'hi'];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With