Ramda.js R.unless() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Logic

Definition

Ramda’s R.unless() answers a guarded-transform question: should this value pass through or take the fallback transformer? You pass a predicate and a function. You get back one function. Call that leftover with a value. Native JavaScript might write pred(x) ? x : whenFalseFn(x). unless names that guard once, curries it, and only runs the transformer on a miss. Drop the leftover into R.map and R.pipe.

The official sample is the whole idea. Increment only when the value is not nil. null stays null. Need a transform only when the test passes? That is R.when. Need both branches to always transform? That is R.ifElse. Need more than two branches? That is R.cond. Previous Logic helper is R.propSatisfies. Next is R.until for iterative loops until a predicate is satisfied.

Syntax

Call R.unless. Start with this line.

Ramda
R.unless(pred, whenFalseFn, x)
CallWhat it does
R.unless(pred, whenFalseFn, x)Primary signature.
R.unless(...)Call the logic helper.

Prefer import { unless } from "ramda" when you want a named import.

Library declarations
Ramda
R.unless(pred, whenFalseFn, x)

Parameters

ParameterDescription
predRequired. Function. A predicate. Called with the same arguments you later pass to the leftover. Truthy result returns x unchanged.
whenFalseFnRequired. Function. Runs only when the predicate is falsy. Accepts Ramda's placeholder R.__.
xRequired. Any. The value to test. On a pass you get this value back. On a miss you get the transformer result.

Return Value

Return ValueDescription
a | bEither the original x on a pass, or whatever whenFalseFn returns on a miss.

Ramda Examples

📚 Getting Started

The official Ramda safe-increment sample, then the same idea compared with when, ifElse, and cond.

Example 1 — Official Safe Increment

Increment only when the value is not nil. null passes through unchanged.

Ramda
const safeInc = R.unless(R.isNil, R.inc);

safeInc(null);
//=> null

safeInc(1);
//=> 2
Try It Yourself

How It Works

R.isNil is the predicate. Nil values pass, so null comes back as null. Non-nil values miss, so R.inc runs. That is the docs sample from ramdajs.com.

Example 2 — vs when, ifElse, and cond

unless is the inverse of when. ifElse always picks a branch. cond is the list form.

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

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

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

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

How It Works

when would transform 50 only if it were 0 — 50 is not 0, so 50 passes through. unless transforms the miss: 50 is not 0, so R.inc runs. R.cond is the same fork as a list of pairs.

📈 Practical Patterns

Pass keeps the original, fail runs the transformer, then map rows and pipe vs ternary.

Example 3 — Predicate Pass Keeps Original / Fail Runs Fn

When the predicate passes, x is returned unchanged. When it fails, whenFalseFn runs.

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

R.unless(gtZero, R.inc)(5);
//=> 5

R.unless(gtZero, R.inc)(-3);
//=> -2

R.unless(R.isNil, R.inc)(null);
//=> null

R.unless(R.isNil, R.inc)(10);
//=> 11
Try It Yourself

How It Works

5 is greater than 0, so the predicate passes and 5 comes back unchanged. -3 fails, so R.inc runs. The same rule applies to nil checks with R.isNil.

Example 4 — Map Rows with Status

Empty objects stay unchanged. Non-empty rows get status: 'ok'.

Ramda
const rows = [
  {},
  { id: 1 },
  { id: 2, name: 'Ada' }
];

const addStatus = R.unless(R.isEmpty, R.assoc('status', 'ok'));

R.map(addStatus, rows);
//=> [{}, { id: 1, status: 'ok' }, { id: 2, name: 'Ada', status: 'ok' }]
Try It Yourself

How It Works

R.isEmpty passes for {}, so the empty row is unchanged. Non-empty rows miss, so R.assoc('status', 'ok') runs. One leftover maps over the whole list.

Example 5 — Pipe Step vs Manual Ternary

The unless leftover slots into R.pipe the same way a hand-written null guard would.

Ramda
const safeInc = R.unless(R.isNil, R.inc);
const piped = R.pipe(safeInc);
const manual = x => x == null ? x : x + 1;

piped(5);
//=> 6

piped(null);
//=> null

manual(5);
//=> 6

manual(null);
//=> null
Try It Yourself

How It Works

R.pipe(safeInc) is the point-first style. The manual ternary does the same nil guard inline. unless names the pattern once so the pipe stays readable.

Browser Support

R.unless() is ordinary JavaScript once Ramda is loaded. Logos use the shared browser-image-sprite.png sprite from this project.

✓ Ramda 0.18.0+ · Any JS runtime

Ramda.js R.unless()

Load Ramda 0.18.0 or later via CDN or npm, then call R.unless with a predicate and transformer. No extra browser API is required.

100% With Ramda 0.18.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.unless() Universal

Bottom line: Safe in any project that includes Ramda 0.18.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.unless(pred, whenFalseFn, x) takes a predicate and a transformer. If pred(x) is truthy, x is returned unchanged. If pred(x) is falsy, whenFalseFn(x) runs. Added in v0.18.0. Type: (a → Boolean) → (a → b) → a → a | b. Arity 3, curried.
safeInc = R.unless(R.isNil, R.inc). safeInc(null) is null. safeInc(1) is 2. Nil values pass through. Non-nil values get incremented.
unless transforms only on a miss and keeps the original on a pass. when transforms only on a pass. ifElse always runs one of two transformers. cond is a list of [predicate, transformer] pairs for many branches.
No. On a pass you get the original x back. On a miss you get whatever whenFalseFn returns: a number, object, string, or anything else.
Yes. when transforms on a pass and otherwise returns x. unless transforms on a miss and otherwise returns x. Same predicate, opposite side of the fork.
No. R.unless is a stable Ramda logic helper from v0.18.0. It is not deprecated, experimental, or non-standard.

Continue to R.until()

Learn how to repeat a transform until a stop predicate is satisfied with Ramda R.until().

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