Multiple logic operators in Javascript

ow 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.
if ( ["apple","banana"].indexOf(a) >= 0){
   // do something
}

Option 2:
Use the in operator:
Version A:
if ( a in { 'apple': '', 'banana': '' } ){
   // do something
}

Version B:
var opt = {
   'apple' : '',
   'banana' : ''
}
if ( a in opt ){
   // do something
}


Last modified by Mohit @ 4/20/2025 1:51:34 PM