Ramda.js R.hasPath() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

Ramda’s R.hasPath() answers a nested question: does this path of own keys exist? You pass a path array, then the object. You get a boolean. Missing a segment is false. A key whose value is undefined is still present. Inherited keys at any step fail.

The official sample is the whole idea. R.hasPath(['a', 'b'], {a: {b: 2}}) is true. The same path is true when b is undefined, false when b is missing, and false on {}. Native JavaScript has no one-liner for this: you would loop hasOwnProperty (or Object.hasOwn) at each step. Need one own key? That is R.has. Need one prototype key? That is R.hasIn. Need the value at the path? That is R.path — and undefined there is ambiguous. Lodash _.has often treats a string as a path; Ramda wants an array of keys.

Syntax

Call R.hasPath. Start with this line.

Ramda
R.hasPath(path, obj)
CallWhat it does
R.hasPath(path, obj)Primary signature.
R.hasPath(...)Call the object helper.

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

Library declarations
Ramda
R.hasPath(path, obj)

Parameters

ParameterDescription
pathRequired. Array of keys. Own keys to walk, such as ['a', 'b'] or ['items', 1]. A dotted string is a literal key, not a nested path. An empty array is always ...
objRequired. Object. The record to query. null and undefined yield false.

Return Value

Return ValueDescription
Booleantrue when every segment is an own property. Otherwise false. The object is not mutated.

Ramda Examples

📚 Getting Started

The official nested path sample, then has and hasIn as one-key cousins.

Example 1 — Official Nested Path

Present keys succeed, including undefined. Missing keys and empty objects fail.

Ramda
R.hasPath(['a', 'b'], { a: { b: 2 } });
//=> true
R.hasPath(['a', 'b'], { a: { b: undefined } });
//=> true
R.hasPath(['a', 'b'], { a: { c: 2 } });
//=> false
R.hasPath(['a', 'b'], {});
//=> false
Try It Yourself

How It Works

b: undefined still owns the key b. { a: { c: 2 } } never had b. That is the docs sample from ramdajs.com.

Example 2 — vs R.has and R.hasIn

has is a one-item path. hasPath does not walk prototypes at nested steps. hasIn does, for one key.

Ramda
const user = { name: 'Ada' };
R.hasPath(['name'], user);
//=> true
R.has('name', user);
//=> true

const proto = { role: 'dev' };
const nested = { user: Object.create(proto) };
nested.user.name = 'Ada';
R.hasPath(['user', 'role'], nested);
//=> false
R.hasIn('role', nested.user);
//=> true
Try It Yourself

How It Works

R.has is hasPath with a one-item array. After user is found, role lives on the prototype, so hasPath says no. R.hasIn uses in on that one object and says yes.

📈 Practical Patterns

Array indexes, empty paths, a null-safe filter, then path as the value-reading cousin.

Example 3 — Arrays, Empty Path, Dotted String

Integer segments target array indexes. An empty path is false. A dotted string is one literal key.

Ramda
const orig = { items: ['a', 'b', 'c'] };
R.hasPath(['items', 1], orig);
//=> true
R.hasPath(['items', 9], orig);
//=> false

const nested = { user: { name: 'Ada' } };
R.hasPath([], nested);
//=> false
R.hasPath(['user.name'], nested);
//=> false
Try It Yourself

How It Works

Dense arrays own their index keys. Index 9 is missing. The source returns false when the path length is 0. 'user.name' would only match a key whose name contains a dot.

Example 4 — Null and filter

null is false, not a throw. Fill the path to keep rows that own a nested key.

Ramda
R.hasPath(['a'], null);
//=> false
R.hasPath(['a'], undefined);
//=> false

const hasName = R.hasPath(['user', 'name']);
R.filter(hasName, [
  { user: { name: 'Ada' } },
  { user: {} },
  { n: 'x' }
]);
//=> [{ user: { name: 'Ada' } }]
Try It Yourself

How It Works

A nil object fails before any key is read. R.filter keeps only the row that owns both user and name.

Example 5 — vs R.path

hasPath is existence. path reads the value, so a present undefined looks the same as a missing key.

Ramda
const present = { a: { b: undefined } };
R.hasPath(['a', 'b'], present);
//=> true
R.path(['a', 'b'], present);
//=> undefined

const missing = { a: { c: 2 } };
R.hasPath(['a', 'b'], missing);
//=> false
R.path(['a', 'b'], missing);
//=> undefined
Try It Yourself

How It Works

Both R.path calls print undefined. Only hasPath tells them apart. Next Object helper is invert.

Browser Support

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

✓ Ramda 0.26.0+ · Any JS runtime

Ramda.js R.hasPath()

Load Ramda 0.26.0 or later via CDN or npm, then call R.hasPath with a path array and an object. No extra browser API is required.

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

Bottom line: Safe in any project that includes Ramda 0.26.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.hasPath(path, obj) returns true when every segment of path is an own property, walking nested objects and arrays. Inherited keys at any step fail. Added in v0.26.0.
R.hasPath(['a','b'], {a:{b:2}}) is true. The same path is true when b is undefined, false when b is missing, and false on {}.
has is a one-key own check. hasIn also walks the prototype for that one key. hasPath walks a path array and uses an own-property check at every step. R.has(prop, obj) is implemented as R.hasPath([prop], obj).
Yes. {a:{b:undefined}} still has own key b, so R.hasPath(['a','b'], obj) is true. Existence is not truthiness.
An empty path [] is false. R.hasPath(['a'], null) and R.hasPath(['a'], undefined) are false. A string such as 'a.b' is one literal key, not a nested path.
No. R.hasPath is a stable Ramda object helper from v0.26.0. It is not deprecated, experimental, or non-standard.

Continue to R.invert()

Learn how to flip an object’s keys and values using Ramda R.invert().

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