Ramda’s R.propOr() answers a one-key question with a fallback: what value sits at this property, or else this default? You pass the default, the key, then the object. You get the property, or the default if it is missing or empty. Native JavaScript’s cousin is obj?.name ?? 'anon'. That is data-first. propOr is data-last, so R.propOr('anon', 'name') is already a function of one object — ready for R.map or R.pipe.
The official sample is the whole idea. Alice is {name: 'ALICE', age: 101}. She has no favoriteLibrary. R.prop('favoriteLibrary')(alice) is undefined. R.propOr('Ramda', 'favoriteLibrary')(alice) is 'Ramda'. Need nested keys with a default? That is R.pathOr. Need the leftover without a default? Stay on R.prop. Need several keys? That is R.props.
Syntax
Call R.propOr. Start with this line.
Ramda
R.propOr(val, p, obj)
Call
What it does
R.propOr(val, p, obj)
Primary signature.
R.propOr(...)
Call the object helper.
Prefer import { propOr } from "ramda" when you want a named import.
Library declarations
Ramda
R.propOr(val, p, obj)
Parameters
Parameter
Description
val
Required. Any. The default. Used when the property is missing or the value is undefined, null, or NaN.
p
Required. String (or number). The property name. Integers work as array indexes through R.prop / nth.
obj
Required. Object or Array. The source. null and undefined yield the default. The source is never mutated.
Return Value
Return Value
Description
Any
The property value, or val when the lookup fails or defaultTo treats the leftover as empty. Falsy values such as 0 and false are kept.
Ramda Examples
📚 Getting Started
The official Alice sample, then the defaultTo rules for zeros, empties, null, and NaN.
Example 1 — Official Alice Favorite Library
A missing property is undefined with prop. The same lookup with propOr returns the default.
Alice has name and age, but no favoriteLibrary. R.prop leaves that hole as undefined. R.propOr runs the same lookup, then defaultTo swaps in 'Ramda'. That is the docs sample from ramdajs.com.
Example 2 — Keep 0 and false, Fall Back on null
defaultTo treats null, undefined, and NaN as empty. Real zeros, false, and empty strings stay.
The test is v == null || v !== v. 0, false, and '' fail that test, so they are kept. null, undefined, and NaN pass it, so you get the default. Do not mix this with ||, which would also replace 0.
📈 Practical Patterns
Compare cousins, then a curried helper, then a null object and an array index.
Example 3 — vs prop, pathOr, and defaultTo
prop leaves undefined. propOr fills it. pathOr is nested. defaultTo is the same swap after you already have the leftover.
prop and propOr share the lookup. Only the leftover differs. R.pathOr is the nested cousin. Internally, propOr is exactly defaultTo(val, prop(p, obj)), so the last line matches the second.
Example 4 — Curry and Map
Fill the default and the key to map over rows. Missing names become 'anon'.
Because propOr is arity 3, filling the default and the key leaves a function of one object. R.prop('name') would put undefined (and null) in the last two slots. propOr keeps the list full of strings.
Example 5 — Null Object, Missing Key, Array Index
A null object does not throw. A missing key uses the default. An integer key reads an array slot through prop / nth.
Native null.name throws. R.prop on null is undefined, so propOr returns the default. Index 0 uses nth just like R.prop. Index 9 is off the list, so you get -1. Nested keys still belong on R.pathOr.
Compatibility
Browser Support
R.propOr() is ordinary JavaScript once Ramda is loaded. Logos use the shared browser-image-sprite.png sprite from this project.
✓ Ramda 0.6.0+ · Any JS runtime
Ramda.js R.propOr()
Load Ramda 0.6.0 or later via CDN or npm, then call R.propOr with a default, a property name, and an object. No extra browser API is required.
100%With Ramda 0.6.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.propOr()Universal
Bottom line: Safe in any project that includes Ramda 0.6.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.propOr(default, key, obj) returns that property if it is present and not null, undefined, or NaN. Otherwise it returns default. Added in v0.6.0. The original object is not mutated.
On alice = {name: 'ALICE', age: 101}, R.prop('favoriteLibrary')(alice) is undefined. R.propOr('Ramda', 'favoriteLibrary')(alice) is 'Ramda'.
prop returns undefined on a miss. propOr substitutes a default. pathOr is the nested-path cousin. Internally propOr is defaultTo(val, prop(p, obj)).
Yes. defaultTo treats undefined, null, and NaN as empty. 0, false, and '' are kept.
No. R.prop on null is undefined, so propOr returns the default. Native null.x throws.
No. R.propOr is a stable Ramda object helper from v0.6.0. It is not deprecated, experimental, or non-standard.