Is there a faster way of writing OR operator?

前端 未结 6 1667
自闭症患者
自闭症患者 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:27

    I just added this one to the jsperf: the in operator. The value in every key:value (e.g. 7:7) pair is irrelevant here.

    if ($('#id').val() in {7:7, 8:8, 9:9}) {
      console.log('value of #id is 7, 8, or 9!')
    };
    

    see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in

提交回复
热议问题