The bitwise operator ^ (XOR) can also be used to evaluate if only one of both given expressions is true and can be a useful shorthand:
x ^ y === Number(x && !y || !x && y)
Since it is not a logical operator, it will cast to Number (0 = false, 1 = true), but if will accept any true-ish value, so you only need to cast to boolean if you actually require the result to be one.
Unfortunately, there's no logical XOR operator in JS as of now.
The bitwise operator
^
(XOR) can also be used to evaluate if only one of both given expressions is true and can be a useful shorthand:Since it is not a logical operator, it will cast to Number (0 = false, 1 = true), but
if
will accept any true-ish value, so you only need to cast to boolean if you actually require the result to be one.Unfortunately, there's no logical XOR operator in JS as of now.
Thanks for your input Alex! You have made more robust the post with your comment, I appreciate you took the time.