short hand for chaining logical operators in javascript?

后端 未结 7 1430
眼角桃花
眼角桃花 2021-01-12 03:13

Is there a better way to write the following conditional in javascript?

if ( value == 1 || value == 16 || value == -500 || value == 42.42 || value == \'somet         


        
7条回答
  •  时光取名叫无心
    2021-01-12 03:50

    In an effort to make yet another way of doing it...

    if (/^(1|16|-500|42.42|something)$/.test(value)) {
      // blah blah blah
    }
    

    No need to extend array prototypes or anything, just use a quick regexp to test the value!

提交回复
热议问题