I have some js code that currently looks something like this:
var test
//do some stuff with the var
if (test === "A" || test === "B" || test === "C") {
if (test === "A" || test === "B") {
//do some small stuff
}
//do some more complex stuff
}
Is there a nicer way to write this to avoid the duplicated condition? It seems basic, I know, but I can't quite put my finger on it.
you could assign a variable to avoid having to write the complex condition multiple times:
let a_or_b = test === 'A' || test === 'B';
if (a_or_b || test === 'C') {
if (a_or_b) {
// do some small stuff
}
// do more complex stuff
}
or
let a_or_b = false;
if (test === 'A' || test === 'B') {
// do some small stuff
a_or_b = true;
}
if (a_or_b || test === 'C') {
// do more complex stuff
}