JavaScript - if语句中“Or”的缩写

时间:2016-09-15 20:57:14

标签: javascript

在if语句中是否有“Or”条件的简写,我觉得重复是愚蠢的,在下面的情况下,“a”变量重复检查所有“Or”条件:

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

    //execute something here

 }

1 个答案:

答案 0 :(得分:3)

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