kanji0816
kanji0816

Reputation: 1

Are there non-obvious cases where (!x || x) can evaluate to false in Javascript?

I just got handed down an older JavaScript project (the original devs already left the company and are not available). Recently I found a condition like this:

const xPermanent = await Promise(/* some call to a Sequelize 5.8.x object */)
const xMulti = await Promise(/* some call to a Sequelize 5.8.x object */)

// lots of code which occasionally checks but never modifies xMulti

if (xPermanent && (!xMulti || xMulti)) {

I'm no expert in Javascript. As I understand, this always evaluates to true if xPermanent is true. Are there any edge cases in which this could evaluate to false other than xPermanent being false?

My normal understanding of logic would say, that I can remove the second part of the condition, but I also heard Javascript does weird things evaluating objects sometimes...

Upvotes: 0

Views: 57

Answers (2)

Keith
Keith

Reputation: 24181

Welcome to the world of Javascript :)

You are looking for an edge case, if xMulti is mutable, then it could be false. eg. Proxy's & getters allow mutation.

The below is an example using a getter, of course this is just plain stupid code, but JS allows you to do these sort of things.. :)

Looking at your code though, it looks like your OK, as those consts are not getters or proxys.

let x = 1;

class Silly {
  get xMulti() { return x--; }
}

with (new Silly()) {
  console.log(`true && (!xMulti || xMulti)`);
  if (true && (!xMulti || xMulti)) {
    console.log(`It's true`);
  } else {
    console.log(`It's false`);
  }
}

Upvotes: 1

Greg Burghardt
Greg Burghardt

Reputation: 18783

The test for the xMulti variable is completely irrelevant. It coerces xMulti into a truthy or falsey value, and says "pass this part of the test if it is true or false". Since a boolean can only be true or false, logic dictates that (!xMulti || xMulti) is superfluous.

At one time there might have been different logic, but as it stands, remove that code with prejudice.

A simple test for if (xPermanent) will suffice.

Upvotes: 2

Related Questions