Ramda.js R.defaultTo() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Logic

Definition

Ramda’s R.defaultTo() answers a fallback question: keep this value, unless it is missing in a strict sense. Missing here means null, undefined, or NaN. You pass the default first. You pass the value second. You get the value back when it is present, or the default when it is not. Native JavaScript val || 42 also replaces 0, false, and ''. defaultTo does not. Drop the leftover into R.map and pipes.

The official sample is the whole idea. defaultTo42(null) is 42. defaultTo42(false) is still false. parseInt('string') is NaN, so that call is 42 too. Need OR of two values? That is R.or. Need a default on an object field? That is R.propOr. Previous Logic helper is R.cond. Next is R.either.

Syntax

Call R.defaultTo. Start with this line.

Ramda
R.defaultTo(default, val)
CallWhat it does
R.defaultTo(default, val)Primary signature.
R.defaultTo(...)Call the logic helper.

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

Library declarations
Ramda
R.defaultTo(default, val)

Parameters

ParameterDescription
defaultRequired. Any. The fallback. Returned when val is null, undefined, or NaN.
valRequired. Any. The candidate leftover. Returned as-is unless it is one of those three missing values.

Return Value

Return ValueDescription
a | bThe second argument when it is present. The first argument when the second is null, undefined, or NaN.

Ramda Examples

📚 Getting Started

The official Ramda defaultTo42 sample, then the same idea compared with || and R.or.

Example 1 — Official defaultTo42

Name the leftover once. null, undefined, and NaN become 42. false and a string stay.

Ramda
const defaultTo42 = R.defaultTo(42);

defaultTo42(null);
//=> 42

defaultTo42(undefined);
//=> 42

defaultTo42(false);
//=> false

defaultTo42('Ramda');
//=> 'Ramda'

defaultTo42(parseInt('string'));
//=> 42
Try It Yourself

How It Works

That is the docs sample from ramdajs.com. parseInt('string') is NaN, so the default wins. false is falsy in JavaScript, but it is not one of the three missing values, so it stays.

Example 2 — vs || and R.or

On 0, defaultTo keeps the zero. || and or replace it.

Ramda
R.defaultTo(42, 0);
//=> 0

0 || 42;
//=> 42

R.or(0, 42);
//=> 42

R.defaultTo(42, '');
//=> ''
Try It Yourself

How It Works

0 is a real count. defaultTo keeps it. || and R.or treat it as missing. An empty string is the same story: defaultTo keeps ''.

📈 Practical Patterns

NaN versus a real zero, mapping a leftover over a list, then a field fallback with propOr.

Example 3 — NaN Falls Back, Zero Does Not

Failed number parses become the default. A legitimate 0 is kept.

Ramda
const toCount = R.pipe(Number, R.defaultTo(0));

toCount('3');
//=> 3

toCount('oops');
//=> 0

toCount('0');
//=> 0

R.defaultTo(0, NaN);
//=> 0
Try It Yourself

How It Works

Number('oops') is NaN, so defaultTo returns 0. Number('0') is the number 0, which is kept. That is why parse helpers pair so well with this leftover.

Example 4 — Map a Default Over a List

Fill holes in a list. Real zeros stay. Missing leftovers become 0.

Ramda
R.map(R.defaultTo(0), [1, null, 0, undefined, 4]);
//=> [1, 0, 0, 0, 4]
Try It Yourself

How It Works

Filling the default leaves a unary function for R.map. Index 2 is a real 0, so it stays. null and undefined become 0. The same list with x || 0 would look identical here, but that is luck: a list of flags with false would not.

Example 5 — Object Field With propOr

defaultTo on a plucked field is R.propOr. A missing name uses the fallback. A present empty string stays if you pluck then defaultTo.

Ramda
const user = { name: '', age: null };

R.propOr('guest', 'role', user);
//=> 'guest'

R.defaultTo('guest', user.name);
//=> ''

R.defaultTo(0, user.age);
//=> 0
Try It Yourself

How It Works

role is missing, so propOr returns 'guest'. name is '', which defaultTo keeps. age is null, so the leftover is 0. Pick propOr when the field might be absent. Pick defaultTo when you already have the leftover in hand.

Browser Support

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

✓ Ramda 0.10.0+ · Any JS runtime

Ramda.js R.defaultTo()

Load Ramda 0.10.0 or later via CDN or npm, then call R.defaultTo with a default and a value. No extra browser API is required.

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

Bottom line: Safe in any project that includes Ramda 0.10.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.defaultTo(default, val) returns val unless val is null, undefined, or NaN. In those three cases it returns default. Added in v0.10.0. Type: a → b → a | b. Arity 2, curried.
defaultTo42 = R.defaultTo(42). defaultTo42(null) is 42. defaultTo42(undefined) is 42. defaultTo42(false) is false. defaultTo42('Ramda') is 'Ramda'. defaultTo42(parseInt('string')) is 42 because parseInt yields NaN.
|| and R.or treat every falsy leftover as missing, so 0, '', and false become the fallback. defaultTo only replaces null, undefined, and NaN. 0, false, and '' are kept.
No. 0, false, and '' are valid leftovers. Only null, undefined, and NaN trigger the default.
NaN is the only value that is not equal to itself (NaN !== NaN). Ramda treats that as missing, the same as null and undefined.
No. R.defaultTo is a stable Ramda logic helper from v0.10.0. It is not deprecated, experimental, or non-standard.

Continue to R.either()

Learn how to OR exactly two predicates with Ramda R.either().

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