Is there anyway to implement XOR in javascript

前端 未结 18 666
孤城傲影
孤城傲影 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:50

    XOR just means "are these two boolean values different?". Therefore:

    if (!!isEmptyString(firstStr) != !!isEmptyString(secondStr)) {
        // ...
    }
    

    The !!s are just to guarantee that the != operator compares two genuine boolean values, since conceivably isEmptyString() returns something else (like null for false, or the string itself for true).

提交回复
热议问题