What is “x && foo()”?

前端 未结 5 1862
遇见更好的自我
遇见更好的自我 2020-11-22 13:34

I saw somewhere else said,

x && foo();

 is equal to

if(x){
    foo();
}

I tested it and they really di

5条回答
  •  渐次进展
    2020-11-22 13:46

    This is known as short-circuit evaluation.

    In this case, if x is False, then foo() doesn't need to be evaluated (the result of && will always be False); if x is True, it does need to be evaluated (even if the result is thrown away).

提交回复
热议问题