为什么JavaScript中没有逻辑异或?


当前回答

Javascript有一个按位的异或运算符:^

var nb = 5^9 // = 12

你可以将它与布尔值一起使用,它会将结果作为0或1(你可以将其转换回布尔值,例如result = !!(op1 ^ op2)。但正如John所说,它等价于result = (op1 != op2),这更清楚。

其他回答

试试这个简短易懂的

function xor(x,y){return true==(x!==y);}

function xnor(x,y){return !xor(x,y);}

这适用于任何数据类型

将值转换为布尔形式,然后按位XOR:

Boolean(a) ^ Boolean(b) // === 0 | 1

注意,这个表达式的结果是一个数字,而不是一个布尔值。

位XOR也适用于非布尔值,但请记住,这是位操作符,而不是逻辑操作符。使用非bool可能不会像你预想的那样:

(5 ^ 3) === 6 // true

在Typescript中(+变为数值):

value : number = (+false ^ +true)

So:

value : boolean = (+false ^ +true) == 1

为了子孙后代的利益,也因为我发现这是一个很好的练习,您可以很容易地利用XOR操作符来强制使用真实性。就像被选中的答案一样,它可能有点太聪明了。

const xor = (a, b) => !!(!!a ^ !!b)

console.log(undefined ^ {}) // Returns 0, bitwise can't be done here.
console.log(xor(undefined, {})) // Returns true, because {} is truthy and undefined is falsy
console.log(0 ^ 1) // Works naturally, returns 1
console.log(xor(0, 1)) // Also works, returns true
console.log(true ^ false) // Again, returns true
console.log(xor(true, false)) // And again, returns true...

为了好玩,这应该在TypeScript中工作,通过强制显式的any:

const xor = (a: any, b: any) => !!((!!a as any) ^ (!!b as any))

Javascript有一个按位的异或运算符:^

var nb = 5^9 // = 12

你可以将它与布尔值一起使用,它会将结果作为0或1(你可以将其转换回布尔值,例如result = !!(op1 ^ op2)。但正如John所说,它等价于result = (op1 != op2),这更清楚。