Ramda’s R.pathOr() answers a nested question with a fallback: what value sits at this trail of keys, or else this default? You pass the default, a path array, then the object. You get the nested value, or the default if the walk fails. Native JavaScript’s cousin is obj?.a?.b ?? 'N/A'. That is data-first. pathOr is data-last, so R.pathOr('anon', ['user', 'name']) is already a function of one object — ready for R.map or R.pipe.
The official sample is the whole idea. R.pathOr('N/A', ['a', 'b'], {a: {b: 2}}) is 2. The same path on {c: {b: 2}} is 'N/A'. Integers mix in for arrays, just like R.path. Need one key with a default? That is R.propOr. Need the leftover without a default? Stay on R.path. Need to know if the path exists? That is R.hasPath. Need to write the path? That is R.assocPath.
Syntax
Call R.pathOr. Start with this line.
Ramda
R.pathOr(d, path, obj)
Call
What it does
R.pathOr(d, path, obj)
Primary signature.
R.pathOr(...)
Call the object helper.
Prefer import { pathOr } from "ramda" when you want a named import.
Library declarations
Ramda
R.pathOr(d, path, obj)
Parameters
Parameter
Description
d
Required. Any. The default. Used when the path is missing or the value is undefined, null, or NaN.
path
Required. Array. Keys to walk. Strings (and symbols) are object keys. Integers are array indexes, including negatives. An empty path returns obj itself unles...
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 value at the tail of path, or d when the walk fails or defaultTo treats the leftover as empty. Falsy values such as 0 and false are kept.
Ramda Examples
📚 Getting Started
The official nested sample, then the same helper on an array inside an object.
Example 1 — Official Nested Path with a Default
A present trail returns the value. A missing first key returns the default without throwing.
Ramda walks a, then b. The second object has no a, so the walk stops and 'N/A' is used. Native {c: {b: 2}}.a.b would throw. That is the docs sample from ramdajs.com.
Example 2 — Array Indexes and Negatives
An integer segment means “this node is a list.” A missing index still returns the default.
Path ['a', 'b', 0] means: key a, key b, then array index 0. -2 uses R.nth from the end. Index 9 is off the list, so the leftover is undefined and the default -1 appears. Pass a number, not '0'.
📈 Practical Patterns
Compare cousins, then a curried helper, then empty paths and the defaultTo edge cases.
Example 3 — vs path, propOr, and hasPath
path leaves undefined. pathOr fills it. propOr is one key. hasPath still says a stored undefined exists, while pathOr still falls back.
path and pathOr share the walk. Only the leftover differs. propOr is the one-key version of the same default idea. R.hasPath is existence, not value: own key b can be present and still be undefined, so pathOr still returns 'N/A'.
Example 4 — Curry and Map
Fill the default and the path to map over rows. Missing names become 'anon'.
Because pathOr is arity 3, filling the default and the path leaves a function of one object. R.path(['user', 'name']) would put undefined in the last two slots. pathOr keeps the list full of strings.
Example 5 — Empty Path, Zero, Dotted String, NaN
An empty path returns the whole value. 0 is kept. A dotted string is one literal key. NaN falls back because of defaultTo.
An empty array never walks, so you get the input back (unless it is null). 0 is not null, so it is not replaced. ['user.name'] looks for a key whose name includes a dot. Split it: ['user', 'name']. NaN !== NaN, so defaultTo treats it as empty and returns 'N/A'.
Compatibility
Browser Support
R.pathOr() 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.pathOr()
Load Ramda 0.18.0 or later via CDN or npm, then call R.pathOr with a default, a path array, and an object. No extra browser API is required.
100%With Ramda 0.18.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.pathOr()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.pathOr(default, path, obj) walks a nested path and returns that value. If the path is missing, or the value is undefined, null, or NaN, it returns default instead. Added in v0.18.0.
R.pathOr('N/A', ['a', 'b'], {a: {b: 2}}) is 2. R.pathOr('N/A', ['a', 'b'], {c: {b: 2}}) is 'N/A'.
path returns undefined on a miss. pathOr substitutes a default. propOr is the one-key cousin. hasPath tests own-key existence, so a stored undefined still counts as present, while pathOr still falls back.
Yes. The source is defaultTo(d)(path(p, obj)). defaultTo treats undefined, null, and NaN as missing. 0, false, and '' are kept.
No. A missing segment, null, or undefined along the way yields the default. That is safer than obj.a.b, which throws when a is missing.
No. R.pathOr is a stable Ramda object helper from v0.18.0. It is not deprecated, experimental, or non-standard.