0

Is there a shorthand for "Or" condition inside an if statement, I feel it's stupid to be repetitive, in the below case "a" variable is repetitive to check all the "Or" conditions:

  if(a != 'Cancelled' || a != 'Rejected' || a != 'Delivered' || a != 'Confirmed'){

    //execute something here

 }
Hamza L.
  • 1,783
  • 4
  • 25
  • 47
  • not really, the best way could potentially be to rewrite the condition using and or just a single flag if possible. – bmartin Sep 15 '16 at 21:00
  • Another one with basically the same title: http://stackoverflow.com/questions/11127753/shorthand-for-multiple-or-expressions-in-if-statement Search before you post. – epascarello Sep 15 '16 at 21:01
  • `a != 'Cancelled' || a != 'Rejected' || ...` will always be true, since if it would be false then `a` must both be `'Cancelled'` and `'Rejected'` (etc.) and that's not possible. – Frxstrem Sep 15 '16 at 21:03

1 Answers1

2
if (['Cancelled', 'Rejected', 'Delivered', 'Confirmed'].indexOf(a) == -1) {
  // do stuff here
}
davidhu
  • 9,523
  • 6
  • 32
  • 53