You could use the bitwise XOR operator (^
) directly:
if (isEmptyString(firstStr) ^ isEmptyString(secondStr)) {
// ...
}
It will work for your example since the boolean true
and false
values are converted into 1
and 0
because the bitwise operators work with 32-bit integers.
That expression will return also either 0
or 1
, and that value will be coerced back to Boolean by the if
statement.
You should be aware of the type coercion that occurs with the above approach, if you are looking for good performance, I wouldn't recommend you to work with the bitwise operators, you could also make a simple function to do it using only Boolean logical operators:
function xor(x, y) {
return (x || y) && !(x && y);
}
if (xor(isEmptyString(firstStr), isEmptyString(secondStr))) {
// ...
}