Optimize OR in IF

前端 未结 7 807
不思量自难忘°
不思量自难忘° 2021-01-22 01:50

I have a code like this:

if (action == \'John\' || action == \'John Beckham\' || action == \'Henry John\'){
     alert(\'true!\');
}

How do I m

相关标签:
7条回答
  • 2021-01-22 02:56

    In addition of indexOf, you can also use conditional operator in Javascript.

    names = ['John' , 
             'John Beckham', 
             'John Henry' , 
             'Giggs John' , 
             'Scholes John', 
             'John Messi'
    ];
    names.indexOf(action) != -1? alert('True') : alert ('False');
    

    And you want to do more then simple statement do like:

     names.indexOf(action) != -1? doSomethingOnTrue() : doSomethingOnFalse(); 
    
    0 讨论(0)
提交回复
热议问题