Is there anyway to implement XOR in javascript

前端 未结 18 724
孤城傲影
孤城傲影 2020-12-24 10:53

I\'m trying to implement XOR in javascript in the following way:

   // XOR validation
   if ((isEmptyString(firstStr) && !isEmptyString(secondStr)) |         


        
18条回答
  •  太阳男子
    2020-12-24 11:43

    There's a few methods, but the ternary method (a ? !b : b) appears to perform best. Also, setting Boolean.prototype.xor appears to be an option if you need to xor things often.

    http://jsperf.com/xor-implementations

提交回复
热议问题