Is there a faster way of writing OR operator?

前端 未结 6 1724
自闭症患者
自闭症患者 2021-01-13 08:31

Is there a faster way of writing this?

if ($(\'#id\').val()==7 || $(\'#id\').val()==8 || $(\'#id\').val()==9){
    console.log(\'value of #id is 7, 8, or 9!\         


        
6条回答
  •  灰色年华
    2021-01-13 09:18

    You could do a switch:

    switch($('#id').val()){
        case 7:
        case 8:
        case 9:
            console.log('value of #id is 7, 8, or 9!');
            break;
        default:
            console.log('value of #id is NOT 7, 8, or 9!')
    }
    

提交回复
热议问题