Ramda’s R.either() answers a logic question: does at least one of these two tests pass for the same arguments? You pass two functions. You get back one function. Call that leftover with a value (or several values). Native JavaScript would write f(x) || g(x). either names that OR once, curries it, and short-circuits like ||. Drop the leftover into R.filter and R.find.
The official sample is the whole idea. gt10 checks greater than 10. even checks even. 101 is odd, but it is greater than 10, so it passes. 8 is not greater than 10, but it is even, so it also passes. Need AND of two functions? That is R.both. Need OR of a list that can grow? That is R.anyPass. Need OR of two values, not two functions? That is R.or. Previous Logic helper is R.defaultTo. Next is R.ifElse.
Syntax
Call R.either. Start with this line.
Ramda
R.either(f, g)
Call
What it does
R.either(f, g)
Primary signature.
R.either(...)
Call the logic helper.
Prefer import { either } from "ramda" when you want a named import.
Library declarations
Ramda
R.either(f, g)
Parameters
Parameter
Description
f
Required. Function. First function. Called with the same arguments you later pass to the leftover. A truthy leftover skips g.
g
Required. Function. Second function. Runs only when f is falsy. Accepts Ramda's placeholder R.__.
Return Value
Return Value
Description
Function
A curried leftover. It returns f(…) || g(…). That can be a boolean, or any leftover JavaScript || would keep (string, number, object).
Ramda Examples
📚 Getting Started
The official Ramda greater-than-10 or even sample, then the same idea compared with both, anyPass, and or.
Example 1 — Official Greater Than 10 or Even
Both functions see the same number. One pass is enough. 101 is odd but greater than 10. 8 is not greater than 10 but even.
Ramda
const gt10 = x => x > 10;
const even = x => x % 2 === 0;
const f = R.either(gt10, even);
f(101);
//=> true
f(8);
//=> true
101 passes gt10, so even is never needed. 8 fails gt10 and still passes because it is even. That is the docs sample from ramdajs.com. 7 would fail both tests.
Example 2 — vs both, anyPass, and or
101 is odd. either is still true. both is false. anyPass is the list form of OR. or never calls a function — it only combines two values.
Ramda
const gt10 = x => x > 10;
const even = x => x % 2 === 0;
R.either(gt10, even)(101);
//=> true
R.both(gt10, even)(101);
//=> false
R.anyPass([gt10, even])(8);
//=> true
R.or(false, true);
//=> true
both needs both tests, so 101 (odd) is false. anyPass with two tests matches either. R.or is a different question: combine two already-known values with ||.
📈 Practical Patterns
Non-boolean leftovers from JavaScript ||, then filter and find, then curry and a placeholder.
Example 3 — Non-Boolean Leftovers
either uses ||, so the leftover is not always true or false. A truthy first function skips the second.
The docs say the leftover is the first function if it is truthy, otherwise the second function. 'ABC' || true keeps 'ABC' and skips the second side. 0 || 42 keeps 42. That is ordinary JavaScript ||.
Example 4 — Filter and Find
Build one leftover from two field tests. Keep tickets that are open or on team blue. Find the first match.
Filling the two functions leaves a function of one object for R.filter and R.find. Row 1 matches status. Row 2 matches team. Row 3 matches neither. The same tests with both would keep none of these three rows.
Example 5 — Curry and Placeholder
Fill the first function, wait for the second, then wait for the value. A placeholder can skip the first slot.
Ramda
const gt10 = x => x > 10;
const even = x => x % 2 === 0;
R.either(gt10)(even)(8);
//=> true
R.either(R.__, even)(gt10)(101);
//=> true
R.either(gt10, even)(7);
//=> false
Arity 2 means R.either(gt10) waits for g. The docs note it takes a placeholder, so R.either(R.__, even) waits for f. 7 is not greater than 10 and not even, so the last call is false.
Compatibility
Browser Support
R.either() is ordinary JavaScript once Ramda is loaded. Logos use the shared browser-image-sprite.png sprite from this project.
✓ Ramda 0.12.0+ · Any JS runtime
Ramda.js R.either()
Load Ramda 0.12.0 or later via CDN or npm, then call R.either with two functions. No extra browser API is required.
100%With Ramda 0.12.0+
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.either()Universal
Bottom line: Safe in any project that includes Ramda 0.12.0 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.either(f, g) takes two functions and returns one new function. That leftover calls f and g with the same arguments and returns f(...) || g(...). It is OR for exactly two functions. Added in v0.12.0. Type: (*… → Boolean) → (*… → Boolean) → (*… → Boolean). Arity 2, curried.
gt10 = x => x > 10; even = x => x % 2 === 0; f = R.either(gt10, even). f(101) is true because 101 is greater than 10. f(8) is true because 8 is even. One pass is enough. 7 would be false: not greater than 10 and not even.
either ORs exactly two functions. both ANDs exactly two functions. anyPass ORs a list of functions (use it when the list can grow). or ORs two values, not two functions.
No. It uses JavaScript ||, so the leftover can be any value. The first truthy leftover from f is returned without calling g. If f is falsy, g's leftover is returned. R.either(R.identity, R.always(true))('ABC') is 'ABC'.
No. || short-circuits. If f returns a truthy leftover, g is not called. Put the cheaper test first when one side is expensive.
No. R.either is a stable Ramda logic helper from v0.12.0. It is not deprecated, experimental, or non-standard.