Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: what's the most elegant way to check whether A is equal to B or C?

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?

like image 737
sbichenko Avatar asked Oct 20 '25 14:10

sbichenko


1 Answers

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

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

like image 172
Amirali Esmaeili Avatar answered Oct 23 '25 09:10

Amirali Esmaeili



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!