Is it valid to use && instead of if?

前端 未结 3 1646
萌比男神i
萌比男神i 2021-01-20 01:01

I am using && like this and it works

typeof foo === \'function\' && foo(); //if foo exist then call it

instead

3条回答
  •  再見小時候
    2021-01-20 01:19

    You could use a void operator. This evaluates the expression and returns undefined.

    void (foo && foo());
    

    var foo;
    
    void (foo && foo());
    
    foo = () => console.log('foo');
    
    void (foo && foo());

提交回复
热议问题