Why is there no logical xor in JavaScript?

前端 未结 19 2122
忘了有多久
忘了有多久 2020-11-29 17:17

Why is there no logical xor in JavaScript?

19条回答
  •  鱼传尺愫
    2020-11-29 17:23

    there is... sort of:

    if( foo ? !bar : bar ) {
      ...
    }
    

    or easier to read:

    if( ( foo && !bar ) || ( !foo && bar ) ) {
      ...
    }
    

    why? dunno.

    because javascript developers thought it would be unnecessary as it can be expressed by other, already implemented, logical operators.

    you could as well just have gon with nand and thats it, you can impress every other possible logical operation from that.

    i personally think it has historical reasons that drive from c-based syntax languages, where to my knowledge xor is not present or at least exremely uncommon.

提交回复
热议问题