Ramda.js R.pathOr() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

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)
CallWhat 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

ParameterDescription
dRequired. Any. The default. Used when the path is missing or the value is undefined, null, or NaN.
pathRequired. Array. Keys to walk. Strings (and symbols) are object keys. Integers are array indexes, including negatives. An empty path returns obj itself unles...
objRequired. Object or Array. The source. null and undefined yield the default. The source is never mutated.

Return Value

Return ValueDescription
AnyThe 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
R.pathOr('N/A', ['a', 'b'], {a: {b: 2}});
//=> 2

R.pathOr('N/A', ['a', 'b'], {c: {b: 2}});
//=> 'N/A'
Try It Yourself

How It Works

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.

Ramda
const tree = {a: {b: [1, 2, 3]}};

R.pathOr(-1, ['a', 'b', 0], tree);
//=> 1

R.pathOr(-1, ['a', 'b', -2], tree);
//=> 2

R.pathOr(-1, ['a', 'b', 9], tree);
//=> -1
Try It Yourself

How It Works

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.

Ramda
const miss = {c: {b: 2}};
R.path(['a', 'b'], miss);
//=> undefined
R.pathOr('N/A', ['a', 'b'], miss);
//=> 'N/A'

R.propOr('N/A', 'a', {b: 1});
//=> 'N/A'

const hole = {a: {b: undefined}};
R.hasPath(['a', 'b'], hole);
//=> true
R.pathOr('N/A', ['a', 'b'], hole);
//=> 'N/A'
Try It Yourself

How It Works

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'.

Ramda
const userName = R.pathOr('anon', ['user', 'name']);

R.map(userName, [
  { user: { name: 'Ada' } },
  { user: {} },
  {}
]);
//=> ['Ada', 'anon', 'anon']
Try It Yourself

How It Works

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.

Ramda
R.pathOr('N/A', [], {a: 1});
//=> {a: 1}

R.pathOr(99, ['score'], {score: 0});
//=> 0

R.pathOr('N/A', ['user.name'], { user: { name: 'Ada' } });
//=> 'N/A'

R.pathOr('N/A', ['n'], {n: NaN});
//=> 'N/A'
Try It Yourself

How It Works

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'.

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 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.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.

Continue to R.paths()

Learn how to read several nested paths at once using Ramda R.paths().

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