Multiple logic operators in Javascript
How to determine if a variable matches one of multiple choices in Javascript.
These won't fail, but none will give the results you would likely want:
Case 1:
if (b == 'apple' || 'banana' || 'orange') // do something }
Case 2:
if (b == ('apple' || 'banana' || 'orange')) // do something }
Option 1:
Use the indexOf() method. Note that this method returns 0 or greater if a match is found.
Version A:
if ( ['apple','banana'].indexOf(a) >= 0){ // do something }
Version B:
var opt = ['apple','banana']; if ( opt.indexOf(a) >= 0){ // do something }
Option 2:
Use the in operator:
Version A:
if ( a in { 'apple': '', 'banana': '' } ){ // do something }
Version B: This may be better if you have many options.
var opt = { 'apple' : '', 'banana' : '' } if ( a in opt ){ // do something }
reference: https://stackoverflow.com/questions/22125750/multiple-conditions-in-if-statement-on-both-sides-of-the-logical-operator