Why is there no logical xor in JavaScript?

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

Why is there no logical xor in JavaScript?

19条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 17:44

    Javascript has a bitwise XOR operator : ^

    var nb = 5^9 // = 12
    

    You can use it with booleans and it will give the result as a 0 or 1 (which you can convert back to boolean, e.g. result = !!(op1 ^ op2)). But as John said, it's equivalent to result = (op1 != op2), which is clearer.

提交回复
热议问题