2

this is an embarrassingly basic question but I haven't been able to find anything I understood to be an answer on MDN, W3schools, here, or Google searching.

When referring to multiple conditions, like within an if statement:

if ((a != b) && (a != c) && (a != d)){...}

(considering how nastily long the list can get,) there must be a more efficient way to do it. I imagine it would look similar to this:

if (a != (b || c || d)){...}

That doesn't work, but there must be some other efficient solution. Let me know if you can. Thanks.

EDIT: apparently valuable info: all variables are primitives.

8
  • 3
    It depends on what a and b and c are,.. Commented Jun 19, 2018 at 23:15
  • 5
    Possibly if (![b, c, d].includes(a)) if all are primitives. Commented Jun 19, 2018 at 23:16
  • @JMcFar Yes, like CRise has shown, if they are simple primitives you can place them in an array and use includes.. So for numbers CRise solution will work fine. For none primitives it may even work too, depending on how you use this technique. Commented Jun 19, 2018 at 23:22
  • 1
    @JMcFar strings are primitives. So will work fine too. Commented Jun 19, 2018 at 23:23
  • 2
    @JMcFar If an answer solves your problem you should accept it regardless of who authored it. Danziger's answer has my upvote. We're just glad we could help. Commented Jun 19, 2018 at 23:58

1 Answer 1

4

You can create an Array with all the possible values and use Array.prototype.indexOf() or Array.prototype.includes() to check whether a give value is or is not inside that Array:

const values = [1, 2, 3, 4, 5, 7, 8, 9];

console.log(`[${ values.join(', ') }].indexOf(1) = ${ values.indexOf(1) }`);
console.log(`[${ values.join(', ') }].indexOf(6) = ${ values.indexOf(6) }`);
console.log(`[${ values.join(', ') }].includes(1) = ${ values.includes(1) }`);
console.log(`[${ values.join(', ') }].includes(6) = ${ values.includes(6) }`);

As you can see, indexOf() will return -1 if the value is not inside the Array or the index of its first occurrence otherwise, which might be useful depending on your use case, while includes() only returns true if the value is present or false otherwise, and it's not supported in IE.

So in your case, you would end up with something like this:

const b = 'b';
const c = 'c';
const d = 'd';

const values = [b, c, d];

let target = 'a';

if (!values.includes(target)){
  console.log(`${ target } not in [${ values.join(', ') }]`);
}

target = 'b';

if (values.includes('b')){
  console.log(`${ target } in [${ values.join(', ') }]`);
}

// You could also do [b, c, d].includes(target) if you don't need to reuse that Array.

This will work with primitive values, so string, number, boolean, null, undefined and symbol, but also with Object references, not to be confused with just Object, as you can see below:

const values = [{ foo: 1 }, { bar: 2 }, { baz: 3 }];
const target = { foo: 1 };

console.log(`values.includes(target) = ${ values.includes(target) }`);

// This will not work because the { foo: 1 } object inside values and the one we are passing to
// includes are not the same, as you can see here:

console.log(`values[0] === target = ${ values[0] === target }`);

// However, if we get a reference to it, then those two references can be compared successfully:

const reference = values[0];

console.log(`values[0] === reference = ${ values[0] === reference }`);

// So now we can also use includes or indexOf:

console.log(`values.includes(reference) = ${ values.includes(reference) }`);

If you want to understand how this works, you could try to implement a function similar to indexOf or includes yourself. Something simple like:

function isValueInArray(arr, value) {

  const size = arr.length;
  
  // Iterate over all the indexes in arr:
  for (let i = 0; i < size; ++i) {
  
    // Check whether the value at the current index matches the target value:
    if (arr[i] === value) {
    
      // If so, we have found value at index i in arr, so we return true and stop searching:
      return true;
      
      // Note we could also return the index if we care about the position the value is at.
    }
  }
  
  // We have checked all the values in arr and none of them matched, so we return false: 
  return false;
}

const values = [0, 1, 2, 3, 4, 5, 7, 8, 9];

console.log(`isValueInArray(values, 1) = ${ isValueInArray(values, 1) }`);
console.log(`isValueInArray(values, 6) = ${ isValueInArray(values, 6) }`);

However, keep in mind a real implementation is slightly more complicated. For example, you could take a look at this polyfill for includes.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! This is correct. However I'm in an awkward position of not knowing precise stackoverflow etiquette-- @CRice answered in a comment on my post earlier and solved it for me first, so I'm not sure if I should mark yours as the solution or wait for him to format his to an answer... hrrrmmm :/ EDIT: screw it, you technically did the 'answering' first and it clearly took more of your time, WINNER. thanks again
@JMcFar Thanks (: At the end, the decision is up to you, but you can find some guidelines here stackoverflow.com/help/someone-answers and here meta.stackexchange.com/questions/5234/…. Some users prefer to send a quick and short answer first and then edit it to add more details, while others spend a bit more time writing a detailed one at first. You can also read about that here meta.stackexchange.com/questions/9731/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.