Ramda’s R.ifElse() answers a logic question: should this value take the true branch or the false branch? You pass three functions. You get back one function. Call that leftover with a value. Native JavaScript would write condition(x) ? onTrue(x) : onFalse(x). ifElse names that fork once, curries it, and always runs exactly one transformer. Drop the leftover into R.map and R.pipe.
The official sample is the whole idea. If the object already has count, increment it. If it does not, start at 1. Need a transform only when the test passes, and keep the original otherwise? That is R.when. Need a transform only when the test misses? That is R.unless. Need more than two branches? That is R.cond. Previous Logic helper is R.either. Next is R.isEmpty.
Syntax
Call R.ifElse. Start with this line.
Ramda
R.ifElse(condition, onTrue, onFalse)
Call
What it does
R.ifElse(condition, onTrue, onFalse)
Primary signature.
R.ifElse(...)
Call the logic helper.
Prefer import { ifElse } from "ramda" when you want a named import.
Library declarations
Ramda
R.ifElse(condition, onTrue, onFalse)
Parameters
Parameter
Description
condition
Required. Function. A predicate. Called with the same arguments you later pass to the leftover. Truthy leftover runs onTrue.
onTrue
Required. Function. Runs only when the condition is truthy. Accepts Ramda's placeholder R.__.
onFalse
Required. Function. Runs only when the condition is falsy.
Return Value
Return Value
Description
Function
A curried leftover. It returns whatever onTrue or onFalse returns after the condition is checked.
Ramda Examples
📚 Getting Started
The official Ramda increment-or-start-at-1 sample, then the same idea compared with when, unless, and cond.
Example 1 — Official Increment or Start Count
If the object already has count, increment it. If it does not, start at 1. Only one transformer runs.
R.has('count') is the condition. A present key runs R.over with R.inc. A missing key runs R.assoc('count', 1). That is the docs sample from ramdajs.com.
Example 2 — vs when, unless, and cond
ifElse always picks a branch. when keeps 50 unchanged. unless increments 50. cond is the list form of the same two-way idea.
when does not invent an else branch — 50 comes back as 50. unless transforms the miss. R.cond is the same fork as a list of pairs, with R.T as the default.
📈 Practical Patterns
Even / odd labels, mapping pass / fail over scores, then curry and a placeholder.
Example 3 — Even / Odd Labels
The leftover is not a boolean. Each branch returns a sentence. Only one sentence is built.
Ramda
const isEven = n => n % 2 === 0;
const label = R.ifElse(
isEven,
n => n + ' is even',
n => n + ' is odd'
);
label(4);
//=> '4 is even'
label(5);
//=> '5 is odd'
Arity 3 means R.ifElse(pred) waits for onTrue, then onFalse. R.ifElse(R.__, onTrue, onFalse) waits for the condition. -3 is not greater than 0, so the last call returns the false-branch leftover 0.
Compatibility
Browser Support
R.ifElse() is ordinary JavaScript once Ramda is loaded. Logos use the shared browser-image-sprite.png sprite from this project.
✓ Ramda 0.8.0+ · Any JS runtime
Ramda.js R.ifElse()
Load Ramda 0.8.0 or later via CDN or npm, then call R.ifElse with three functions. No extra browser API is required.
100%With Ramda 0.8.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.ifElse()Universal
Bottom line: Safe in any project that includes Ramda 0.8.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.ifElse(condition, onTrue, onFalse) takes three functions and returns one new function. That leftover calls condition with the same arguments. If the leftover is truthy, onTrue runs. If it is falsy, onFalse runs. Added in v0.8.0. Type: (*… → Boolean) → (*… → *) → (*… → *) → (*… → *). Arity 3, curried.
incCount = R.ifElse(R.has('count'), R.over(R.lensProp('count'), R.inc), R.assoc('count', 1)). incCount({ count: 1 }) is { count: 2 }. incCount({}) is { count: 1 }. The object either already has count, so it increments, or it gets count: 1.
ifElse always runs one of two transformers. when transforms only on a pass and otherwise returns the original value. unless transforms only on a miss. cond is a list of [predicate, transformer] pairs when you have more than two branches.
No. The leftover is whatever onTrue or onFalse returns: an object, a string, a number, anything. The condition is the only function treated as a yes/no test.
Yes. The docs say ifElse takes its arity from the longest of the three functions passed to it. The leftover is curryN of that max length.
No. R.ifElse is a stable Ramda logic helper from v0.8.0. It is not deprecated, experimental, or non-standard.