I'm trying to implement XOR in javascript in the following way:
// XOR validation if ((isEmptyString(firstStr) && !isEmptyString(secondStr)) || (!isEmptyString(firstStr) && isEmptyString(secondStr)) { alert(SOME_VALIDATION_MSG); return; }
Is there a better way to do this in javascript?
Thanks.
The logical XOR operator is not present in C++ because it is simply an equivalent, not equal to operator with Boolean values.
The ^ (bitwise XOR) in C or C++ takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different. The << (left shift) in C or C++ takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.
As others have pointed out, logical XOR is the same as not-equal for booleans, so you can do this:
// XOR validation if( isEmptyString(firstStr) != isEmptyString(secondStr) ) { alert(SOME_VALIDATION_MSG); return; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With