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)
Call
What 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
Parameter
Description
default
Required. Any. The fallback. Returned when val is null, undefined, or NaN.
val
Required. Any. The candidate leftover. Returned as-is unless it is one of those three missing values.
Return Value
Return Value
Description
a | b
The 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.
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.
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.
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.
Compatibility
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 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.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.