Is it valid to use && instead of if?

前端 未结 3 1647
萌比男神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条回答
  •  梦毁少年i
    2021-01-20 01:30

    In the background there is a bit more with the && thingy, but for 99% of the cases it is perfectly fine to do it that way, just like this one.

    Now as a personal opinion, for a one-liner I prefer it the && way instead of the if one, because I can't stand if keyword without a block below it hehe.

    If you know what you are doing, linters are too picky sometimes.

提交回复
热议问题