short hand for chaining logical operators in javascript?

后端 未结 7 1466
眼角桃花
眼角桃花 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:53

    nope, that is the shorthand.

    as an alternative, you can do a switch

    switch (value) {
    case 1 :
    case 16 :
    case -500 :
        ....
    }
    

    which is easier to manage if you need a lot of possible values, but actually your version is shorter anyway :)

提交回复
热议问题