Ramda.js R.ifElse() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Logic

Definition

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

ParameterDescription
conditionRequired. Function. A predicate. Called with the same arguments you later pass to the leftover. Truthy leftover runs onTrue.
onTrueRequired. Function. Runs only when the condition is truthy. Accepts Ramda's placeholder R.__.
onFalseRequired. Function. Runs only when the condition is falsy.

Return Value

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

Ramda
const incCount = R.ifElse(
  R.has('count'),
  R.over(R.lensProp('count'), R.inc),
  R.assoc('count', 1)
);

incCount({ count: 1 });
//=> { count: 2 }

incCount({});
//=> { count: 1 }
Try It Yourself

How It Works

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.

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

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

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

R.cond([
  [R.equals(0), R.always('freeze')],
  [R.T, R.always('other')]
])(0);
//=> 'freeze'
Try It Yourself

How It Works

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

How It Works

4 is even, so the true branch runs. 5 is odd, so the false branch runs. The leftover of ifElse is whatever that branch returns, not true / false.

Example 4 — Map Pass / Fail

Build one leftover. Map it over scores. 60 and above pass.

Ramda
const grade = R.ifElse(
  R.gte(R.__, 60),
  R.always('pass'),
  R.always('fail')
);

R.map(grade, [90, 45, 60]);
//=> ['pass', 'fail', 'pass']
Try It Yourself

How It Works

R.gte(R.__, 60) waits for a number, then asks n >= 60. Filling the three ifElse slots leaves a function of one number for R.map. 60 is a pass.

Example 5 — Curry and Placeholder

Fill the condition first, wait for the two transformers, then wait for the value. A placeholder can skip the first slot.

Ramda
const pred = R.gt(R.__, 0);

R.ifElse(pred)(R.always('pos'))(R.always('not'))(5);
//=> 'pos'

R.ifElse(R.__, R.always('pos'), R.always('not'))(pred)(-1);
//=> 'not'

R.ifElse(pred, R.identity, R.always(0))(-3);
//=> 0
Try It Yourself

How It Works

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.

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

Continue to R.isEmpty()

Learn how to test empty values with Ramda R.isEmpty().

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