Ramda’s R.cond() answers a branching question: which of these tests passes first, and what should we return then? You pass an array of pairs. Each pair is [predicate, transformer]. You get back one function. Call that leftover with a value (or several values). Native JavaScript would write a chain of if / else if. cond names that chain once, keeps both sides as functions, and stops at the first truthy predicate. Drop the leftover into R.map.
The official sample is the whole idea. Equals 0 → water freezes. Equals 100 → water boils. R.T is always true, so 50 falls through to “nothing special.” Need exactly one test and two branches? That is R.ifElse. Need to transform only when a test passes? That is R.when. Need to transform only when a test fails? That is R.unless. Previous Logic helper is R.complement. Next is R.defaultTo.
Syntax
Call R.cond. Start with this line.
Ramda
R.cond(pairs)
Call
What it does
R.cond(pairs)
Primary signature.
R.cond(...)
Call the logic helper.
Prefer import { cond } from "ramda" when you want a named import.
Library declarations
Ramda
R.cond(pairs)
Parameters
Parameter
Description
pairs
Required. Array. A list of [predicate, transformer] pairs. Both items in each pair must be functions. An empty array is allowed.
Return Value
Return Value
Description
Function
A curried leftover. It returns the transformer leftover for the first truthy predicate. If no predicate matches, it returns undefined.
Ramda Examples
📚 Getting Started
The official Ramda water sample, then the same idea compared with ifElse, when, and unless.
Example 1 — Official Freeze, Boil, and Default
Each pair is two functions. R.always ignores the temperature. The last pair uses R.T so every leftover has an answer.
Ramda
const fn = R.cond([
[R.equals(0), R.always('water freezes at 0°C')],
[R.equals(100), R.always('water boils at 100°C')],
[R.T, temp => 'nothing special happens at ' + temp + '°C']
]);
fn(0);
//=> 'water freezes at 0°C'
fn(50);
//=> 'nothing special happens at 50°C'
fn(100);
//=> 'water boils at 100°C'
water freezes at 0°C
nothing special happens at 50°C
water boils at 100°C
How It Works
0 matches the first pair. 100 matches the second. 50 matches neither, so R.T runs the default transformer with 50. That is the docs sample from ramdajs.com.
Example 2 — vs ifElse, when, and unless
ifElse is two branches. when transforms only on a pass. unless transforms only on a miss. cond is the list form.
when at 50 returns 50 unchanged. unless at 50 increments because the test failed. Use those cousins when you have one test. Use cond when the list of tests can grow.
📈 Practical Patterns
No default pair, mapping a leftover over a list, then pair order so the first match wins.
Example 3 — No Match Returns undefined
Without R.T, a miss has no transformer. The leftover is undefined. Add a default pair when every call needs an answer.
50 matches neither pair, so the loop finishes with no leftover. An empty pair list never loops, so it is also undefined. Put [R.T, …] last when a miss should still return something.
Example 4 — Map Grades Over a List
Build one leftover from score bands. Map it over several scores. Put the default last.
R.gte(R.__, 90) waits for the score. 95 matches A first. 82 skips A, then matches B. 70 matches C. 40 falls through to F. Pair order is the band order.
Example 5 — First Match Wins
A broader test above a narrower one hides the later pair. Put specific predicates first.
R.T is always true, so it must sit last. If it sits first, every call takes the default and later pairs never run. Same rule as putting default last in a switch — except both sides here are still functions.
Compatibility
Browser Support
R.cond() is ordinary JavaScript once Ramda is loaded. Logos use the shared browser-image-sprite.png sprite from this project.
✓ Ramda 0.6.0+ · Any JS runtime
Ramda.js R.cond()
Load Ramda 0.6.0 or later via CDN or npm, then call R.cond with an array of [predicate, transformer] pairs. No extra browser API is required.
100%With Ramda 0.6.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.cond()Universal
Bottom line: Safe in any project that includes Ramda 0.6.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.cond(pairs) takes a list of [predicate, transformer] pairs and returns one function. That leftover applies the same arguments to each predicate in order. The first truthy predicate wins, and its transformer runs with those arguments. If nothing matches, the leftover is undefined. Added in v0.6.0. Type: [[(*… → Boolean), (*… → *)]] → (*… → *).
fn = R.cond([[R.equals(0), R.always('water freezes at 0°C')], [R.equals(100), R.always('water boils at 100°C')], [R.T, temp => 'nothing special happens at ' + temp + '°C']]). fn(0) is the freeze message. fn(50) is the default. fn(100) is the boil message.
cond is a list of if/else pairs. ifElse is exactly one test plus a then and an else. when runs a transformer only if the test passes, otherwise returns the value. unless runs a transformer only if the test fails.
The leftover is undefined. Put [R.T, transformer] last as a default so every call has an answer. R.T is a function that is always true.
No. The docs say both elements of every pair must be functions, and cond itself returns a function. You cannot put a raw string or number in a pair. Use R.always('text') when the leftover should ignore the arguments.
No. R.cond is a stable Ramda logic helper from v0.6.0. It is not deprecated, experimental, or non-standard.