Ramda.js R.last() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.last(list) returns the final item of a list, or the final character of a string (official Ramda docs). It never throws on an empty list — it returns undefined — and it does not change the list.

Added in v0.1.4: R.last is stable — not deprecated, experimental, or non-standard. Version note: since v0.31.0, R.last('') returns undefined; v0.30.x and earlier return ''.

Syntax

Call R.last. Start with this line.

Ramda
R.last(list) (one argument) Type [a] → a | undefined
CallWhat it does
R.last(list) (one argument) Type [a] → a | undefinedPrimary signature.
R.last(...)Call the list helper.

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

Library declarations
Ramda
R.last(list) (one argument) Type [a] → a | undefined

Parameters

ParameterDescription
listRequired. An array, string, or other array-like value with a length

Return Value

Return ValueDescription
*The final item, or undefined when the list is empty

Ramda Examples

Example 1 — Official Samples

Read the end of a list, an empty list, and a string — the samples from the Ramda docs.

Ramda
console.log(R.last(['fi', 'fo', 'fum']));   // 'fum'
console.log(R.last([]));                    // undefined
console.log(R.last('abc'));                 // 'c'
Try It Yourself

How It Works

  1. List. The list has 3 items, so R.last reads index 2: 'fum'.
  2. Empty. There is no item to read, so the result is undefined — no error is thrown.
  3. String. A string is read like a list of characters, so 'abc' gives 'c'.
  4. Unchanged. None of the inputs are modified.

Example 2 — Get a File Extension

Split a file name on dots and keep the last piece.

Ramda
const extension = R.pipe(R.split('.'), R.last);

console.log(extension('photo.final.png'));   // 'png'
console.log(extension('README'));            // 'README'
Try It Yourself

How It Works

  1. Split. R.split('.') turns 'photo.final.png' into ['photo', 'final', 'png'].
  2. Last. R.last picks 'png', even though the name has two dots.
  3. Pipeline. R.pipe joins both steps into one reusable extension function.
  4. Watch out. 'README' has no dot, so the only piece — the whole name — comes back.

Example 3 — Last Item That Matches

Find the last even number — with filter plus last, or in one step with findLast.

Ramda
const nums = [1, 2, 3, 4, 5];
const isEven = (n) => n % 2 === 0;

console.log(R.last(R.filter(isEven, nums)));   // 4
console.log(R.findLast(isEven, nums));         // 4
Try It Yourself

How It Works

  1. Filter. R.filter(isEven) keeps [2, 4].
  2. Last. R.last picks 4 from that new list.
  3. One step. R.findLast searches from the end and returns 4 without building a filtered list.
  4. No match. Both return undefined when nothing passes the test.

Example 4 — Player with the Highest Score

Sort records from low to high, then take the last one.

Ramda
const players = [
  { name: 'Ada', score: 3 },
  { name: 'Linus', score: 9 },
  { name: 'Grace', score: 5 }
];

const topPlayer = R.pipe(R.sortBy(R.prop('score')), R.last, R.prop('name'));

console.log(topPlayer(players));   // 'Linus'
Try It Yourself

How It Works

  1. Sort. R.sortBy(R.prop('score')) orders the players 3, 5, 9 in a new array.
  2. Last. R.last picks the highest-scoring record, { name: 'Linus', score: 9 }.
  3. Name. R.prop('name') reads 'Linus'.
  4. Original safe. R.sortBy returns a copy, so players keeps its order.

Example 5 — last vs head vs nth(-1) vs init

Four helpers on the same list — see which return an item and which return a list.

Ramda
const nums = [1, 2, 3];

console.log(R.last(nums));       // 3
console.log(R.head(nums));       // 1
console.log(R.nth(-1, nums));    // 3
console.log(R.init(nums));       // [1, 2]
Try It Yourself

How It Works

  1. last. Returns the final item, 3.
  2. head. R.head returns the first item, 1.
  3. nth(-1). R.nth(-1) counts back one from the end — exactly what R.last does.
  4. init. R.init returns a list of everything except the last item.

Browser & Runtime Support

R.last() has been in Ramda since v0.1.4. It only reads length and one index, so it works in every JavaScript engine — only the empty-string result depends on your Ramda version.

✓ Ramda 0.1.4+ · Any JS runtime

Ramda.js R.last()

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% Any Ramda version
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.last() Universal

Bottom line: Safe in every Ramda version and every modern or legacy runtime. Unlike native array.at(-1), it needs no ES2022 support. Load ramda.min.js from a CDN, or npm install ramda for Node, Deno, and Bun. These labs load [email protected].

Frequently Asked Questions

R.last(list) returns the last item of a list, or the last character of a string. The official samples are R.last(['fi', 'fo', 'fum']), which is 'fum', and R.last('abc'), which is 'c'.
R.last([]) returns undefined instead of throwing. The same undefined comes back when the last item really is undefined, so check R.isEmpty first if you need to tell them apart.
It depends on the Ramda version. From v0.31.0, R.last('') returns undefined. In v0.30.x and earlier it returns an empty string '', because those versions used String#charAt.
R.head returns the first item and R.last the final one. R.init returns every item except the last, as a new list. R.nth(-1, list) gives the same result as R.last(list).
For arrays, yes: both return the final item or undefined. R.last is a ready-made function, so it can be passed straight to R.pipe, R.map, or other helpers without writing an arrow function.
No. R.last has been part of Ramda since v0.1.4 and is stable. It is not deprecated, experimental, or non-standard; only its empty-string result changed in v0.31.0.

Next: R.lastIndexOf()

Find the position of the last matching value in a list.

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