Search code examples
javascriptif-statementternary-operator

In JavaScript, how do you check "a" against "b," "c," etc. in a simple manner?


Currently, the only way I know of to do what the title states is by writing the following:

var a = 3, b = 5, c = 3;
if (a === b && a === c) {
  // code
}

Or by using the ternary operator:

(a === b && a === c) ? /* code */ : /* else */

Is there a way to check a against both b and c? Something like this perhaps:

if (a === (b && c)) {
  // code
}

Obviously this doesn't work as intended, which is why I'm asking the question. Any help is appreciated.

This is not a duplicate of the other 2 - those two are using the OR operator. I'm asking about the AND operator.


Solution

  • For a simple case like this? Definitely not. There are some handy little tricks you can use if you build an array, however. For example:

    var my_array = [3, 5, 3];
    if(my_array.every(function(el) {return el == my_array[0];})) {
        // code
    }