3

I need to evaluate a variable and check if it's equal to one of two other variables.

I do it like this: if (a == b || a == c) { }

I feel there's got to be a better way to evaluate variables in this manner. Is there?

sbichenko
  • 12,442
  • 7
  • 44
  • 83
  • 1
    Come on, this is counter productive. – Salman A Jul 04 '12 at 17:21
  • 3
    Could you please explain why? – sbichenko Jul 04 '12 at 17:21
  • Judging by the the comments later on, this may be a duplicate of http://stackoverflow.com/questions/237104/array-containsobj-in-javascript – robbrit Jul 04 '12 at 17:25
  • Well, this is actually another question. I just felt that the way I did it was somewhat redundant, but this is probably because I have no theoretical background in CS. – sbichenko Jul 04 '12 at 17:26
  • @exizt: look at the answers; writing a function, loop, checking browser support, including a library; need I say more. – Salman A Jul 04 '12 at 17:32
  • I was actually hoping for a language construct like `if (a == (b || c))` (I realize why that can't work in JS), but I guess it might be redundant. – sbichenko Jul 04 '12 at 17:34

5 Answers5

4

If you're just comparing one value to 2 others, this is the most efficient and elegant solution. No need to change anything!

If you want to see whether one variable is equal to any of a larger collection of other variables you can do something like:

if(([b,c,d,e,f].indexOf(a) !== -1) {
  // "a" is equal to either b, c, d, e, or f
}

The indexOf method may not be available in older browsers so be sure to check for it! In legacy browsers you can use write your own method to search through the array or utilize any popular general-use library... most will have this functionality.

Cecchi
  • 1,525
  • 9
  • 9
  • What if I need to compare it to more values, 5 or 6, for example? – sbichenko Jul 04 '12 at 17:23
  • 2
    Simply make an array containing all of the values you want to compare against, and call the `indexOf` method on that array, with the one value you wish to compare against as the only argument. – Cecchi Jul 04 '12 at 17:27
3

You can also use Array.prototype.includes (ES7).

if ([b, c, d, e].includes(a)) {
  // do stuff
}

2

.. And if you have many variables to compare against, you could probably use this:

if ([a,b,c,d,e,f,g,h].indexOf(yourValue) >= 0)

There's one more way of doing it, that is in case you are willing to do a little more typing:

if (yourValue in {a:0,b:0, c:0, ... })

or may be you could have a function that returns the object version of the array:

var wrap = function(){
    var obj = {};
    for (var i=0; i<arguments.length; i++)
        obj[arguments[i]]=0;
    return obj;
};

and then:

if (yourValue in wrap(a,b,c,d,e,f,g))

If you are fine with jQuery, you could use jQuery's way:

$.inArray(yourValue, [a,b,c,d,e,f])
UltraInstinct
  • 41,605
  • 12
  • 77
  • 102
0

I think that what you have above is the most straightforward option.

Also, in JavaScript there are different kinds of equal operators. The == equal operator returns a boolean true if both the operands are equal. JavaScript will attempt to convert different data types to the same type in order to make the comparison. The === operator is the strict equal operator and only returns a Boolean true if both the operands are equal and of the same type.

Esailija
  • 134,577
  • 23
  • 263
  • 318
Trey Combs
  • 710
  • 5
  • 10
0
var values = [b,c,d,e,f];

for (var i=0,n=values.length; i<n; i++){
   if (values[i] == a){
       // ...
       break;
   }
}
vol7ron
  • 38,313
  • 20
  • 110
  • 168
  • But for two values this definitely would be an overkill, wouldn't it? – sbichenko Jul 04 '12 at 17:27
  • 1
    Most definitely. For two values the example in the original question is best. This is best for a variant amount of possibilities, or if the number of possibilities are predetermined, to reduce code from being too long and cluttered. – vol7ron Jul 04 '12 at 17:28