How to make this if statement shorter?

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

    You can use a switch:

    switch (abc) { case 'value1': case 'value2': case 'value3': {
      // do something
    }}
    

    Or written in a more traditional form:

    switch (abc) {
      case 'value1':
      case 'value2':
      case 'value3': {
        // do something
      }
    }
    

提交回复
热议问题