We would have all come across a situation where the number of conditional operators to be used in an expression is more like in the below example.
const firstCondition = true,
secondCondition = true,
thirdCondition = true,
fourthCondition = false,
fifthCondition = true;
if(firstCondition && secondCondition && thirdCondition && fourthCondition && fifthCondition) {
console.log("All the conditions met expectation");
}
else {
console.log("All the conditions did not meet expectation");
}
if(firstCondition || secondCondition || thirdCondition || fourthCondition || fifthCondition) {
console.log("Atleast one of the conditions met expectation");
}
else {
console.log("None of the conditions met the expectation");
}
//Output:
//All the conditions did not meet the expectation
//At least one of the conditions met expectation
Code quality tools like ESLint, SonarQube, etc will actually suggest us to optimize this code to have fewer conditional operators. So how do we do that?
There are many ways to solve this. I am going to give a simpler way (Updated). If you have an easier or better solution please feel free to leave it in the comment section.
const firstCondition = true,
secondCondition = true,
thirdCondition = true,
fourthCondition = false,
fifthCondition = true;
const conditionsToCheck = [firstCondition, secondCondition, thirdCondition, fourthCondition, fifthCondition]
if(conditionsToCheck.every(condition => condition)) {
console.log("All the conditions met expectation");
}
else {
console.log("All the conditions did not meet expectation");
}
if(conditionsToCheck.some(condition => condition)) {
console.log("Atleast one of the conditions met expectation");
}
else {
console.log("None of the conditions met the expectation");
}
//Output:
//All the conditions did not meet the expectation
//At least one of the conditions met expectation
We can also mix in conditions like below and it would work without any problem.
const conditionsToCheck = [firstCondition, secondCondition || sixthCondition, thirdCondition, fourthCondition, fifthCondition && seventhCondition];
Unless you are supporting IE8 and below you should fine using Array some and every methods.
I hope this article is helpful.
Top comments (6)
Hi @kvharish , I like what you are trying to do, however wouldn't it be better to make use of the array functions like
every
orsome
?, that way you eliminate the need for an additional function:And just because I love lazy evaluation you could even turn those conditions into predicates:
That way conditions are not evaluated until you need them to.
Regards
Definitely agree using
every
andsome
are the way to go. You could simplify further by usingBoolean
like so,conditionArray.every(Boolean)
Nice, haven't tried it, however, won't it evaluate as true for
Boolean
being an object?Absolutely we can use every and some.
Another way to group conditions is to group them in arrays where
for all conditions: [conditionsToCheck].every(x => x)
for at least one: [conditionsToCheck].some(x => x)
Yes, we can use it.
More optimized way.