[removed] compare variable against array of values

前端 未结 5 876
滥情空心
滥情空心 2021-02-06 07:12

In javascript I am doing the following which works fine.

if (myVar == 25 || myVar == 26 || myVar == 27 || myVar == 28)
 {
   //do something
 }

5条回答
  •  醉话见心
    2021-02-06 07:20

    Other way :

    myVar = (myVar == parseInt(myVar) ? myVar : false); //to check if variable is an integer and not float
    if ( myVar >= 25 && myVar <= 28){}
    

    Live demo

    Edit based on the comment of Anthony Grist

    This way works if you know what those values are going to be (i.e. they're not dynamic) and your array contains a series of consecutive numeric values.

提交回复
热议问题