Why '&&' and not '&'?

前端 未结 16 1563
别跟我提以往
别跟我提以往 2020-11-27 12:11

Why is && preferable to & and || preferable to |?

I asked someone who\'s been programming for years a

16条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 13:15

    && is the short circuit version of &.

    If we are evaluating false & true, we already know from looking at the first argument that the result will be false. The && version of the operator will return a result as soon as it can, rather than evaluate the whole expression. There is also a similar verion of the | operator, ||.

提交回复
热议问题