Logical xor operator
True when exactly one of two values is truthy.
function xor([a, b]) { return Boolean(a) !== Boolean(b); }
Input & expected output
Input
[
true,
false
]Expected output
trueThe edge case
Input
[
"",
"y"
]Expected output
trueKnow the limits
- Operands are converted to booleans first. The original’s (a && !b) || (!a && b) returns an operand instead: for the edge case it returns "y", not true.
- The lab takes the two values as an array.
The function above is the implementation used by the lab. The lab validates JSON types and caps size before execution; include equivalent validation when accepting untrusted input in your application.
Source & maintenance
Based on Logical xor operator from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.
Returns a boolean; the original could return one of its operands instead of true or false. Original MDX retained as inert source evidence.
Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.
Keep exploring
- Identity function — Return the argument unchanged.
- Box handler — Chain transformations on a value, then unwrap the result.
- Check if a value is a function — True for anything callable: functions, arrows, async and generator functions, classes.