How to make this if statement shorter?

后端 未结 6 570
执念已碎
执念已碎 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:43

    You could use a switch statement. Like this:

    switch(abc) {
        'value1':
        'value2':
        'value3':
             // do something
             break;
        default:
             // noop
    }
    

    But your original if with || is probably still preferable.

提交回复
热议问题