Ramda.js R.cond() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Logic

Definition

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

ParameterDescription
pairsRequired. Array. A list of [predicate, transformer] pairs. Both items in each pair must be functions. An empty array is allowed.

Return Value

Return ValueDescription
FunctionA 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'
Try It Yourself

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.

Ramda
R.ifElse(R.equals(0), R.always('freeze'), R.always('other'))(0);
//=> 'freeze'

R.when(R.equals(0), R.always('freeze'))(0);
//=> 'freeze'

R.when(R.equals(0), R.always('freeze'))(50);
//=> 50

R.unless(R.equals(0), R.inc)(50);
//=> 51
Try It Yourself

How It Works

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.

Ramda
const withoutDefault = R.cond([
  [R.equals(0), R.always('freeze')],
  [R.equals(100), R.always('boil')]
]);

withoutDefault(0);
//=> 'freeze'

withoutDefault(50);
//=> undefined

R.cond([])(99);
//=> undefined
Try It Yourself

How It Works

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.

Ramda
const grade = R.cond([
  [R.gte(R.__, 90), R.always('A')],
  [R.gte(R.__, 80), R.always('B')],
  [R.gte(R.__, 70), R.always('C')],
  [R.T,             R.always('F')]
]);

R.map(grade, [95, 82, 70, 40]);
//=> ['A', 'B', 'C', 'F']
Try It Yourself

How It Works

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.

Ramda
const tooBroad = R.cond([
  [R.T,           R.always('default')],
  [R.equals(0),   R.always('freeze')]
]);

const specificFirst = R.cond([
  [R.equals(0),   R.always('freeze')],
  [R.T,           R.always('default')]
]);

tooBroad(0);
//=> 'default'

specificFirst(0);
//=> 'freeze'
Try It Yourself

How It Works

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.

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 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.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.

Continue to R.defaultTo()

Learn how to replace null and undefined with Ramda R.defaultTo().

R.defaultTo() 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