Ramda.js R.xor() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Logic

Definition

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)
CallWhat 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

ParameterDescription
aRequired. Any. Left-hand value. Tested for JavaScript truthiness. Not returned as-is.
bRequired. Any. Right-hand value. Tested for JavaScript truthiness. Not returned as-is.

Return Value

Return ValueDescription
Booleantrue 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.

Ramda
R.xor(true, true);
//=> false

R.xor(true, false);
//=> true

R.xor(false, true);
//=> true

R.xor(false, false);
//=> false
Try It Yourself

How It Works

That is the docs sample from ramdajs.com. Exclusive disjunction: one truthy and one falsy yields true; both the same yields false.

Example 2 — vs R.or and R.and

or keeps a leftover. xor always returns a Boolean. and needs both truthy.

Ramda
R.or(1, 0);
//=> 1

R.xor(1, 0);
//=> true

R.and(1, 0);
//=> 0

R.xor(true, true);
//=> false
Try It Yourself

How It Works

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.

Ramda
R.xor(1, 0);
//=> true

R.xor('hi', 'ok');
//=> false

R.xor('', 5);
//=> true

R.xor(null, undefined);
//=> false
Try It Yourself

How It Works

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.

Ramda
R.xor(true)(false);
//=> true

R.map(R.xor(true), [true, false]);
//=> [false, true]

R.map(R.apply(R.xor), [[true, false], [true, true], [false, false]]);
//=> [true, false, false]

R.reduce(function (n, pair) {
  return n + Number(R.xor(pair[0], pair[1]));
}, 0, [[true, false], [false, true], [true, true]]);
//=> 2
Try It Yourself

How It Works

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.

Ramda
function exclusiveRole(user) {
  return R.xor(user.admin, user.guest);
}

exclusiveRole({ admin: true, guest: false });
//=> true

exclusiveRole({ admin: false, guest: true });
//=> true

exclusiveRole({ admin: true, guest: true });
//=> false

exclusiveRole({ admin: false, guest: false });
//=> false
Try It Yourself

How It Works

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.

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All 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.

Continue to R.clamp()

Learn how to keep a value inside an inclusive range with Ramda R.clamp().

R.clamp() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful