How can I simplify `(variableA && !variableB) || !variableA` expression in JavaScript?

(variableA && !variableB) || !variableA; if we use factoring to this result below

(!variableA  || variableA) && (!variableA ||!variableB)

first part is always true then only second part is enough for u

!variableA ||!variableB

const variableA = 0;
const variableB = undefined;
console.log((variableA && !variableB) || !variableA); // true
console.log(!variableA ||!variableB);


You could use

!(a && b)

or the equivalent with De Morgan's laws

!a || !b

const
    f = (a, b) => (a && !b) || !a,
    g = (a, b) => !(a && b),
    h = (a, b) => !a || !b

console.log(0, 0, f(0, 0), g(0, 0), h(0, 0));
console.log(0, 1, f(0, 1), g(0, 1), h(0, 1));
console.log(1, 0, f(1, 0), g(1, 0), h(1, 0));
console.log(1, 1, f(1, 1), g(1, 1), h(1, 1));

Tags:

Javascript