Ramda.js R.indexOf() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.indexOf(value, list) returns the position of the first item equal to value, or -1 if there is none. “Equal” means R.equals, so objects and arrays match by contents (official Ramda docs).

Stable since v0.1.0: R.indexOf is not deprecated, experimental, or non-standard. Warning: it returns -1 when nothing matches — check for it before using the result as an index.

Syntax

Call R.indexOf. Start with this line.

Ramda
R.indexOf(value, list)
CallWhat it does
R.indexOf(value, list)Primary signature.
R.indexOf(...)Call the list helper.

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

Library declarations
Ramda
R.indexOf(value, list)

Parameters

ParameterDescription
valueRequired. Any. The item to look for
listOptional. Array. Where to look; omit it to get a reusable function

Return Value

Return ValueDescription
*The value returned by R.indexOf().

Ramda Examples

Example 1 — Official Samples

Where is the number? Both samples from the Ramda docs.

Ramda
console.log(R.indexOf(3, [1, 2, 3, 4]));  // 2
console.log(R.indexOf(10, [1, 2, 3, 4])); // -1
Try It Yourself

How It Works

  1. Value first. The number to find comes before the list.
  2. Count from 0. 1 is at 0, 2 at 1, so 3 is at 2.
  3. Not found. No item equals 10, so the answer is -1.
  4. Always a number. Need only yes or no? R.includes is clearer.

Example 2 — Find an Object by Value

Ramda finds an object with the same contents; native indexOf does not.

Ramda
const colors = [{ name: 'red' }, { name: 'blue' }];

console.log(R.indexOf({ name: 'blue' }, colors)); // 1
console.log(colors.indexOf({ name: 'blue' }));    // -1 (native)
Try It Yourself

How It Works

  1. A new object. { name: 'blue' } in the call is not the same object as the one in colors.
  2. Ramda compares contents. R.equals sees matching keys and values at position 1.
  3. Native compares references. Array.prototype.indexOf uses ===, so it returns -1.
  4. Whole object only. Every key must match; for one field, use R.findIndex.

Example 3 — First Match vs Last Match

The value appears three times. indexOf gives the first, lastIndexOf the last.

Ramda
const xs = [-1, 3, 3, 0, 1, 2, 3, 4];

console.log(R.indexOf(3, xs));     // 1
console.log(R.lastIndexOf(3, xs)); // 6
Try It Yourself

How It Works

  1. Three 3s. They sit at positions 1, 2, and 6.
  2. indexOf. Searches from the start and stops at 1.
  3. lastIndexOf. R.lastIndexOf searches from the end and stops at 6 (the official sample).
  4. Same rules. Both use R.equals and return -1 when nothing matches.

Example 4 — Remove the First Match Safely

Use the position to delete one item, but only when it was found.

Ramda
const removeFirst = (x, xs) => {
  const i = R.indexOf(x, xs);
  return i === -1 ? xs : R.remove(i, 1, xs);
};

console.log(JSON.stringify(removeFirst('b', ['a', 'b', 'c', 'b']))); // ["a","c","b"]
console.log(JSON.stringify(removeFirst('z', ['a', 'b'])));           // ["a","b"]
Try It Yourself

How It Works

  1. Find. 'b' is first at position 1.
  2. Remove one. R.remove(1, 1, xs) deletes that single item; the later 'b' stays.
  3. Guard. 'z' gives -1, so the list is returned unchanged.
  4. Why the guard matters. Without it, R.remove(-1, 1, xs) would delete the last item.

Example 5 — Sort by a Fixed Order

A list’s positions make a handy rank. Fix the list with R.__ and sort by it.

Ramda
const rank = R.indexOf(R.__, ['gold', 'silver', 'bronze']);

console.log(rank('silver')); // 1
console.log(JSON.stringify(R.sortBy(rank, ['bronze', 'gold', 'silver'])));
// ["gold","silver","bronze"]
Try It Yourself

How It Works

  1. Placeholder. R.__ keeps the value slot open, so rank waits for one medal.
  2. Rank. rank('silver') is its position: 1.
  3. Sort. R.sortBy orders medals by rank: 0, 1, 2.
  4. Why needed. R.indexOf('gold') alone would fix the value, not the list.

Browser & Runtime Support

R.indexOf() has been in Ramda since v0.1.0. It is plain JavaScript, so support depends only on loading Ramda — not on the browser’s own indexOf rules.

✓ Ramda 0.1.0+ · Any JS runtime

Ramda.js R.indexOf()

The UMD build (ramda.min.js) is compiled to ES5 syntax, so it runs in Chrome, Firefox, Safari, and Edge. Via npm it works in Node.js, Deno, and Bun with require or import.

100% With Ramda loaded
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
R.indexOf() Universal

Bottom line: Safe in every Ramda version. Results are the same in every runtime, including the NaN and -0 rules above. These labs load [email protected].

Frequently Asked Questions

R.indexOf(value, list) returns the 0-based position of the first item equal to value, or -1 if no item matches. Equality is R.equals, so objects and arrays are compared by their contents.
Native list.indexOf(value) uses strict equality, so a different object with the same shape is not found, and NaN is never found. R.indexOf compares by value with R.equals and finds NaN. It also takes the value first, which makes it easy to curry.
R.indexOf looks for a value. R.findIndex runs a test function on each item. Use findIndex for partial matches, for example R.findIndex(R.propEq('blue', 'name'), colors).
R.indexOf gives the first matching position, R.lastIndexOf gives the last one, and R.includes only answers true or false. All three use R.equals.
-1 means not found, but many Ramda functions treat a negative index as counting from the end. R.remove(-1, 1, list) removes the last item, and R.update(-1, x, list) replaces it. Always test i !== -1 first.
No. R.indexOf has been part of Ramda since v0.1.0 and is stable. It is not deprecated, experimental, or non-standard.

Next: R.init()

Get every item of a list except the last one.

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