1

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.

simplexshotz
  • 139
  • 12
  • 1
    What exactly is wrong with `if (a === b && a === c) {`? – Ram Aug 23 '18 at 22:24
  • Possible duplicate of [Check variable equality against a list of values](https://stackoverflow.com/questions/4728144/check-variable-equality-against-a-list-of-values) – Heretic Monkey Aug 23 '18 at 22:25
  • Possible duplicate of [Javascript: The prettiest way to compare one value against multiple values](https://stackoverflow.com/questions/9121395/javascript-the-prettiest-way-to-compare-one-value-against-multiple-values) – Sebastian Simon Aug 23 '18 at 22:41
  • Both possible duplicates are asking about the OR operator. I'm asking about the AND operator. – simplexshotz Aug 25 '18 at 12:10

3 Answers3

2

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
}
B. Fleming
  • 7,170
  • 1
  • 18
  • 36
  • Or you can use arrow function `if (my_array.every(el => el === my_array[0]))` – Tomas Aug 23 '18 at 22:27
  • @Tomas In modern browsers, definitely. I wanted my solution to account for older browsers for the less fortunate among us. Thank you for providing this alternative solution :) – B. Fleming Aug 23 '18 at 22:30
1

Unfortunately no. That's what you have. Unless you convert it to an Array and test with a loop.

0

if a,b and c are all numbers then you can do simple maths to check for parity of b and c against a.

Simply add b and c, divide by 2 to get the average number and use that to compare against a.

var a = 3, b = 5, c = 3;
  a === (b + c)/2 
  ? console.log('yes')
  : console.log('no')
 
 // console gives "no"
  
var a = 3, b = 3, c = 3;
  a === (b + c)/2 
  ? console.log('yes')
  : console.log('no') 
  
  // console gives "yes"
  
  
  
var a = 3, b = 4, c = 2;
  a === (b + c)/2 
  ? console.log('yes')
  : console.log('no') 
 
 // console gives "yes"
gavgrif
  • 15,194
  • 2
  • 25
  • 27
  • it is not correct for all cases eg: var a = 3, b = 4, c = 2; a === (b + c)/2 ? console.log('yes') : console.log('no') // gives "yes" – Amjad Rehman A Feb 15 '21 at 10:55
  • @AmjadRehmanA - its been a long time since this answer - but it seems to me that your example SHOULD give "yes" as the solution - if a is 3 and b is 4 and c is 2 then a does equal (b + c)/2.... in other words 3 is equal to (4+2)/2 because that equates to (6)/2 or 3... .so the equatino would be is 3 === 3 , which is yes.Answer amended to include your example... – gavgrif Feb 15 '21 at 11:08