Ramda.js R.takeLast() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.takeLast(n, list) returns the last n items of a list, or the last n characters of a string (official Ramda docs). It is the mirror of R.take: same rules, but counted from the end. The original is never changed.

Stable since v0.16.0: R.takeLast is not deprecated, experimental, or non-standard. It is close to native Array.prototype.slice(-n) and String.prototype.slice(-n), but handles n = 0 correctly.

Syntax

Call R.takeLast. Start with this line.

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

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

Library declarations
Ramda
R.takeLast(n, list) or R.takeLast(n)(list)

Parameters

ParameterDescription
nRequired. Number. How many items to keep from the end
listOptional. Array or String. The value to copy from; omit it to get a reusable function

Return Value

Return ValueDescription
*Array → new array, string → new string

Ramda Examples

Example 1 — Lists and Strings (Official Samples)

Keep the last one, two, or more items — or the last characters of a string.

Ramda
const names = ['foo', 'bar', 'baz'];

console.log(R.takeLast(1, names));   // ['baz']
console.log(R.takeLast(2, names));   // ['bar', 'baz']
console.log(R.takeLast(4, names));   // ['foo', 'bar', 'baz']
console.log(R.takeLast(3, 'ramda')); // 'mda'
Try It Yourself

How It Works

  1. One. R.takeLast(1, names) keeps only the last item, still inside an array.
  2. Two. R.takeLast(2, names) keeps the last two, in their original order.
  3. Too many. Asking for 4 of 3 items returns all three — no error.
  4. String. For text, the last 3 characters come back as a string: 'mda'.

Example 2 — Show the Most Recent Log Entries

New entries are appended at the end, so the newest ones are the last items.

Ramda
const logs = ['boot', 'login', 'view', 'click', 'logout'];

console.log(R.takeLast(3, logs)); // ['view', 'click', 'logout']
console.log(logs.length);         // 5
Try It Yourself

How It Works

  1. Newest last. Each new log line was added to the end of logs.
  2. Take three. R.takeLast(3, logs) keeps 'view', 'click', and 'logout'.
  3. Order kept. The items stay oldest-to-newest; use R.reverse to show newest first.
  4. No mutation. logs.length is still 5.

Example 3 — Mask a Card Number

Show only the last four digits of a string.

Ramda
const mask = (card) => '**** ' + R.takeLast(4, card);

console.log(mask('4111111111111111')); // '**** 1111'
console.log(mask('12'));               // '**** 12'
Try It Yourself

How It Works

  1. String in, string out. R.takeLast(4, card) returns the last four characters as text.
  2. Prefix. '**** ' hides the rest of the number.
  3. Short input. '12' has fewer than 4 characters, so all of it is kept — no error.
  4. Display only. Masking is for showing data; never store full card numbers in plain text.

Example 4 — Average of the Last Three Readings

Combine takeLast with R.mean for a “recent average”.

Ramda
const temps = [19, 21, 20, 24, 25];

const recent = R.takeLast(3, temps);

console.log(recent);         // [20, 24, 25]
console.log(R.mean(recent)); // 23
Try It Yourself

How It Works

  1. Pick the window. R.takeLast(3, temps) keeps the three newest readings.
  2. Average. R.mean adds them (69) and divides by 3.
  3. Result. The recent average is 23, ignoring the older, cooler readings.
  4. As one function. R.pipe(R.takeLast(3), R.mean) gives the same result for any list.

Example 5 — Keep a Capped History

Add a new item, then keep only the last three — like a “recently viewed” list.

Ramda
const addToHistory = (item, history) => R.takeLast(3, R.append(item, history));

console.log(addToHistory('d', ['a', 'b', 'c'])); // ['b', 'c', 'd']
console.log(addToHistory('b', ['a']));           // ['a', 'b']
Try It Yourself

How It Works

  1. Append. R.append adds 'd' to the end: ['a', 'b', 'c', 'd'].
  2. Cap. R.takeLast(3, …) keeps the newest three, so the oldest 'a' falls off.
  3. Not full yet. With only two items, nothing is removed.
  4. Immutable. Each call returns a new array, which suits React or Redux state.

Browser & Runtime Support

R.takeLast() is plain JavaScript inside Ramda 0.16.0+. It is built on R.drop and slice, so support depends only on loading Ramda.

✓ Ramda 0.16.0+ · Any JS runtime

Ramda.js R.takeLast()

The UMD build (ramda.min.js) uses ES5 syntax, so it runs in every modern browser. 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 End of life · ES5 build may load, untested
Legacy only
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Node.js Supported · npm install ramda
Supported
Deno Supported · npm:ramda
Supported
Bun Supported · bun add ramda
Supported
R.takeLast() Universal

Bottom line: Safe in any project that loads Ramda 0.16.0 or later. Use ramda.min.js from a CDN in the browser, or npm install ramda for Node, Deno, and Bun. The Try-it labs load [email protected] and use modern syntax (const, arrow functions).

Frequently Asked Questions

A new list with the last n items, or a new string with the last n characters. R.takeLast(2, ['foo', 'bar', 'baz']) returns ['bar', 'baz'] and R.takeLast(3, 'ramda') returns 'mda'. The original is not changed.
You get the whole list as a copy, with no error. R.takeLast(4, ['foo', 'bar', 'baz']) returns all three items.
They match for most n, but list.slice(-0) returns the whole list while R.takeLast(0, list) correctly returns []. R.takeLast is also curried, so R.takeLast(3) is a reusable function.
It returns the whole list. R.takeLast(-1, [1, 2, 3]) and R.takeLast(NaN, [1, 2, 3]) are both [1, 2, 3].
No. Unlike R.take, R.takeLast needs the whole list first. R.into([], R.takeLast(2), list) silently returns every item. Run the transducer first, then call R.takeLast on the result.
No. R.takeLast has been part of Ramda since v0.16.0 and is stable. It is not deprecated, experimental, or non-standard.

Next: R.takeLastWhile()

Take items from the end of a list while a test passes.

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