How to make this if statement shorter?

后端 未结 6 500
执念已碎
执念已碎 2021-01-28 04:57

Can I make this statement shorter?

if(abc==\'value1\' || abc==\'value2\' || abc==\'value3\') {//do something}

to make it look similar to this:<

6条回答
  •  春和景丽
    2021-01-28 05:53

    if (['value1', 'value2', 'value3'].indexOf(abc) != -1)
    

    This one manages to remain somewhat readable, but you should leave your original code as-is unless you have way more conditions.

提交回复
热议问题