Search code examples
javascriptlogic

Refactoring A && B && B > A


There is a way to refactor this logic expression?

A && B && B > A

Just learning and I would like to know another way of writing the same expression Thank you


Solution

  • Split the expressions and put them in variables with readable names.

    Due to the lack of information about what A and B are, I assume the first half of the expression makes sure both values are truthy and then you compare them:

    const bothTruthy= A && B;
    const BLargerThanA = B > A;
    
    const yourExpression = bothTruthy && BLargerThanA;