Why is there no logical xor in JavaScript?

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

Why is there no logical xor in JavaScript?

19条回答
  •  [愿得一人]
    2020-11-29 17:44

    How about transforming the result int to a bool with double negation? Not so pretty, but really compact.

    var state1 = false,
        state2 = true;
        
    var A = state1 ^ state2;     // will become 1
    var B = !!(state1 ^ state2); // will become true
    console.log(A);
    console.log(B);

提交回复
热议问题