最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

Please see this minimum example:

const result = (variableA && !variableB) || !variableA;

In this expression, I can't simply write this

const result = variableA && !variableB;

Because if variableA = 0, the result will be different

const variableA = 0;
const variableB = undefined;

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

Is there any way I can simplify this expression?

Please see this minimum example:

const result = (variableA && !variableB) || !variableA;

In this expression, I can't simply write this

const result = variableA && !variableB;

Because if variableA = 0, the result will be different

const variableA = 0;
const variableB = undefined;

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

Is there any way I can simplify this expression?

Share Improve this question asked Apr 30, 2020 at 11:39 JosephJoseph 4,7558 gold badges40 silver badges80 bronze badges 3
  • 3 Boolean simplifier: dcode.fr/boolean-expressions-calculator – mateleco Commented Apr 30, 2020 at 11:46
  • !!(variableA && !variableB) you can do this. If your endgoal is to just get a boolean result. – slumbergeist Commented Apr 30, 2020 at 11:55
  • @slumbergeist, the result is alway boolean, please see below. – Nina Scholz Commented Apr 30, 2020 at 12:00
Add a ment  | 

3 Answers 3

Reset to default 8

(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));

Your answer @nina-scholz is not (explicit) aware of the javascript type-casting. Your own acepted answer will only work in your special usecase while using the ! (not) operator.

const variableA = 0;
const variableB = undefined;

console.log(variableA && variableB); // 0
console.log(variableA || variableB); // undefind

This will neither have a boolean result because even using the boolean operators && and || the resulting type is not boolean.

You need to typecast the variables to boolean before logical operation.

The ! (not) operator will do this explicit. If you double it !! you have surely a boolean value to logical pare. This works also for undefined values.

const variableA = 0;
const variableB = undefined;

console.log(!!variableA && !!variableB); // false
console.log(!!variableA || !!variableB); // false

Now your result of the logical operation is always from a logical boolean expression.

Check your question again:

const variableA = 0;
const variableB = undefined;

console.log("no booleans -----");
console.log(variableA); // => 0 - what is NOT a boolean
console.log(variableB); // => undefined - what is NOT a boolean

console.log("explicit booleans with not operator -----");
console.log(!variableA); // => true - now it is a boolean
console.log(!variableB); // => true - now it is a boolean

console.log("explicit booleans with double not operator -----");
console.log(!!variableA); // => false - now it is a boolean
console.log(!!variableB); // => false - now it is a boolean

console.log("with and without typecast -----");
console.log(variableA && !variableB); // => 0 - what is NOT a boolean
console.log(!!variableA && !variableB); // => false - now all are boolean

console.log("your question with and without explicit typecast -----");
console.log(0 || !variableA); // => true
console.log(false || !variableA); // => true
console.log(0 || true); // => true
console.log(false || true); // => true

As @pc-coder has already shown, you may change your current expression

const variableA = 0;
const variableB = undefined;

console.log((variableA && !variableB) || !variableA);

// expand expression

console.log((variableA || !variableA) && (!variableB || !variableA));

// reduce expression while first part (variableA || !variableA) is always true

console.log(!variableB || !variableA);

// the above will work thru automatic typecast with not operator

// or exclude the not operator and typecast explicit

console.log(!(!!variableB && !!variableA));

发布评论

评论列表(0)

  1. 暂无评论