-2

I have a question regarding logical operations in js:

console.log('XXX' !== ('ZZZ' && 'XXX' && 'No  Code Available' && ''))
console.log(('XXX' !== 'ZZZ') && ('XXX' !== 'XXX') && ('XXX' !== 'No  Code Available') && ('XXX' !== ''))

The first one is true, the second one is false. I thought the first and the second is the same statement, but the first one is shorter.

How can I shorten the second one if that is correct?

pqa1222
  • 45
  • 6
  • `'ZZZ' && 'XXX' && 'No Code Available' && ''` outputs `""`. It will always output the last value, because all previous values in the list are truthy. `truthy && truthy && truthy && 'A'` will always yield `'A'`, no matter what exactly is truthy in there. – Jeremy Thille Jan 15 '21 at 15:36

1 Answers1

1

In your second example, all three conditions must be true, for the whole expression to be true. But ('XXX' !== 'XXX') is not true, so the whole expression is false.

lxg
  • 10,238
  • 11
  • 37
  • 60
  • In your first example, you’re comparing the `XXX` string with a boolean – so, yeah. I would suggest reading a bit about operators https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators – lxg Jan 15 '21 at 15:25