Hamza L. Hamza L. - 2 months ago 12
Javascript Question

JavaScript - Shorthand of "Or" inside an if statement

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

}

Answer
if (['Cancelled', 'Rejected', 'Delivered', 'Confirmed'].indexOf(a) == -1) {
  // do stuff here
}