Ramda’s R.xor() answers a logic question: is exactly one of these two values truthy? You pass a, then b. You get back a Boolean: true when one is truthy and the other is falsy, otherwise false. Inclusive OR (R.or) is happy when either or both are truthy and may return a leftover that is not a boolean. Exclusive OR is stricter, and the type is always Boolean. Native bitwise ^ works on 32-bit integers, not on general truthiness. R.xor is a function, arity 2, automatically curried, so R.xor(true) is already a function of one value.
The official sample is the whole idea. Four boolean pairs: mixed flags are true; matching flags are false. Need inclusive OR of two values? That is R.or. Need AND of two values? That is R.and. Previous Logic helper is R.when. Next is R.clamp.
Syntax
Call R.xor. Start with this line.
Ramda
R.xor(a, b)
Call
What it does
R.xor(a, b)
Primary signature.
R.xor(...)
Call the logic helper.
Prefer import { xor } from "ramda" when you want a named import.
Library declarations
Ramda
R.xor(a, b)
Parameters
Parameter
Description
a
Required. Any. Left-hand value. Tested for JavaScript truthiness. Not returned as-is.
b
Required. Any. Right-hand value. Tested for JavaScript truthiness. Not returned as-is.
Return Value
Return Value
Description
Boolean
true when one argument is truthy and the other is falsy. Otherwise false. Neither argument is mutated.
Ramda Examples
📚 Getting Started
The official Ramda boolean table, then the same idea compared with or and and.
Example 1 — Official Boolean Table
When both inputs are booleans, mixed flags are exclusive-true. Matching flags are false.
R.or(1, 0) returns the leftover 1. R.xor(1, 0) coerces that exclusive pair to true. R.and(1, 0) returns the leftover 0 because AND needs both truthy. Previous helper is R.when.
📈 Practical Patterns
Truthy and falsy values that are not true/false, then map and reduce of pairs, then exclusive role flags.
Example 3 — Truthy and Falsy Values
Inputs may be numbers, strings, or nullish. The result is still a Boolean. Mixed truthiness is true; matching truthiness is false.
1 is truthy and 0 is falsy, so xor is true. Both 'hi' and 'ok' are truthy, so xor is false. Empty string is falsy against 5. null and undefined are both falsy, so xor is false.
Example 4 — Curry, Map, and Reduce a Pair List
Fill the first slot. Map that function over flags. Fold a list of pairs with R.reduce.
Because xor is arity 2, filling a leaves a function of one value for R.map. R.apply(R.xor) spreads each pair. Reduce counts how many pairs are exclusive. You cannot pass the bitwise ^ operator into map that way and still get Boolean truthiness checks.
Example 5 — Exclusive Flags (Admin XOR Guest)
Two options where exactly one should be on. Both on or both off fail the exclusive check.
Admin-only and guest-only pass. Both roles at once is inclusive OR territory, not exclusive. Neither role is also false. Inclusive R.or(user.admin, user.guest) would accept the both-on case.
Compatibility
Browser Support
R.xor() is ordinary JavaScript once Ramda is loaded. Logos use the shared browser-image-sprite.png sprite from this project.
✓ Ramda 0.27.1+ · Any JS runtime
Ramda.js R.xor()
Load Ramda 0.27.1 or later via CDN or npm, then call R.xor with two values. No extra browser API is required.
100%With Ramda 0.27.1+
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
R.xor()Universal
Bottom line: Safe in any project that includes Ramda 0.27.1 or later. Use a CDN build such as ramda.min.js in the browser or install ramda from npm for Node. These labs use [email protected].
Frequently Asked Questions
R.xor(a, b) is exclusive disjunction: it returns true when one argument is truthy and the other is falsy. Otherwise it returns false. Added in v0.27.1. Type: a → b → Boolean. Arity 2, curried. The result is always a Boolean.
R.xor(true, true) is false. R.xor(true, false) is true. R.xor(false, true) is true. R.xor(false, false) is false.
or is inclusive OR and returns a || b (a leftover that may not be boolean). and is AND and returns a && b. xor is exclusive: exactly one truthy argument yields true, and the return is always Boolean.
Yes. Unlike R.or, xor never returns a leftover number or string. R.xor(1, 0) is true. R.xor('hi', 'ok') is false. R.xor('', 5) is true.
^ is bitwise XOR on 32-bit integers. R.xor tests JavaScript truthiness of two values, then returns a Boolean. It is also a curried function, so R.xor(true) waits for the second value and R.map(R.xor(true), list) works.
No. R.xor is a stable Ramda logic helper from v0.27.1. It is not deprecated, experimental, or non-standard.