Ramda.js R.lastIndexOf() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.lastIndexOf(value, list) searches from the end of the list and returns the index of the last item equal to value, or -1 if there is none (official Ramda docs). Unlike native Array.prototype.lastIndexOf, it compares with R.equals, so objects with the same contents and NaN can be found.

Stable since v0.1.0: R.lastIndexOf is one of Ramda’s original list functions. It is not deprecated, experimental, or non-standard.

Syntax

Call R.lastIndexOf. Start with this line.

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

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

Library declarations
Ramda
R.lastIndexOf(value, list) or R.lastIndexOf(value)(list)

Parameters

ParameterDescription
valueRequired. The item to find, compared with R.equals
listRequired. Array to search — never changed

Return Value

Return ValueDescription
*Index of the last match, or -1

Ramda Examples

Example 1 — Official Samples

Find the last 3, then look for a value that isn’t there.

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

How It Works

  1. Start at the end. The search begins at index 7 (the value 4).
  2. First hit wins. Index 6 holds 3, so the answer is 6 — the 3s at indexes 1 and 2 are ignored.
  3. Not found. 10 is not in [1, 2, 3, 4], so the walk reaches the start and returns -1.
  4. Zero-based. Like every array index, counting starts at 0.

Example 2 — First vs Last: Spot Duplicates

Compare R.indexOf and R.lastIndexOf to see whether a value appears more than once.

Ramda
const tags = ['js', 'css', 'js', 'html', 'js'];

console.log(R.indexOf('js', tags));     // 0
console.log(R.lastIndexOf('js', tags)); // 4

const isDuplicate = (x, xs) => R.indexOf(x, xs) !== R.lastIndexOf(x, xs);

console.log(isDuplicate('js', tags));  // true
console.log(isDuplicate('css', tags)); // false
Try It Yourself

How It Works

  1. First copy. R.indexOf searches from the start and finds 'js' at 0.
  2. Last copy. R.lastIndexOf searches from the end and finds 'js' at 4.
  3. Duplicate test. If the two indexes differ, the value appears at least twice.
  4. Single copy. 'css' appears once, so both searches return 1 and the test is false.

Example 3 — Objects Are Compared by Value

Native lastIndexOf can’t find a new object with the same contents; Ramda can.

Ramda
const points = [{ x: 1 }, { x: 2 }, { x: 1 }];

console.log(R.lastIndexOf({ x: 1 }, points)); // 2
console.log(points.lastIndexOf({ x: 1 }));    // -1
Try It Yourself

How It Works

  1. A new object. { x: 1 } in the call is a different object from the ones in points.
  2. Deep check. R.equals compares contents, so points[2] matches.
  3. From the end. Index 2 is checked first and matches, so the answer is 2.
  4. Native misses. === compares references, so the native method returns -1.

Example 4 — Finding NaN

Find the last invalid reading in a list of numbers.

Ramda
const readings = [1, NaN, 3, NaN, 5];

console.log(R.lastIndexOf(NaN, readings)); // 3
console.log(readings.lastIndexOf(NaN));    // -1
Try It Yourself

How It Works

  1. The NaN problem. NaN is the only value not equal to itself: NaN === NaN is false.
  2. Native fails. Because native lastIndexOf uses ===, it never finds NaN.
  3. Ramda succeeds. R.equals(NaN, NaN) is true, so the search stops at index 3.
  4. Last one. The NaN at index 1 is never reached because index 3 matches first.

Example 5 — Remove the Last Match

Drop only the last visit to a page, and leave the list alone if the page isn’t there.

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

const history = ['home', 'about', 'home', 'blog'];

console.log(removeLast('home', history)); // ['home', 'about', 'blog']
console.log(removeLast('shop', history)); // ['home', 'about', 'home', 'blog']
Try It Yourself

How It Works

  1. Find it. R.lastIndexOf('home', history) returns 2.
  2. Remove it. R.remove(2, 1, history) returns a new list without that one item.
  3. Guard -1. 'shop' is missing, so i is -1 and the list is returned unchanged.
  4. Why the guard? R.remove(-1, 1, xs) would delete the last item — a silent bug.

Browser & Runtime Support

R.lastIndexOf() is plain JavaScript inside Ramda (available since v0.1.0). It uses no browser API, so support depends only on loading Ramda.

✓ Ramda 0.1.0+ · Any JS runtime

Ramda.js R.lastIndexOf()

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. The arrow functions in some examples need an ES2015+ engine, which every modern browser has.

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.lastIndexOf() Universal

Bottom line: Safe in any project that includes Ramda. Load ramda.min.js from a CDN in the browser, or npm install ramda for Node, Deno, and Bun. These labs load [email protected] from jsDelivr.

Frequently Asked Questions

R.lastIndexOf(value, list) returns the index of the last item equal to value, or -1 if there is none. It compares with R.equals. Official sample: R.lastIndexOf(3, [-1, 3, 3, 0, 1, 2, 3, 4]) returns 6.
Native lastIndexOf compares with ===, so it cannot find NaN or an object with the same contents. R.lastIndexOf uses R.equals on arrays, so R.lastIndexOf({ x: 1 }, [{ x: 1 }]) is 0 and NaN is found. Ramda also takes the value first and has no fromIndex argument.
R.indexOf returns the first matching index and R.lastIndexOf returns the last one. They are the same only when the value appears exactly once, which makes the pair handy for spotting duplicates.
Use R.findLastIndex(fn, list) when you need a test instead of an exact value, such as the last number above 10. R.lastIndexOf(3, list) behaves like R.findLastIndex(R.equals(3), list).
-1 means the value was not found. Always check for it before using the index, because many functions treat -1 as 'the last item' — for example R.remove(-1, 1, list) removes the final element.
No. R.lastIndexOf has been a stable Ramda list function since v0.1.0. It is not deprecated, experimental, or non-standard.

Next: R.length()

Get the number of items in a list, the curried way.

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