Ramda.js R.takeLastWhile() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.takeLastWhile(pred, list) walks the list from the end and keeps items while pred returns true. At the first item that fails, it stops and returns the kept items in their original order (official Ramda docs). Think of it as “the unbroken run at the end of the list”.

Stable since v0.16.0: R.takeLastWhile is a core Ramda list function. It is not deprecated, experimental, or non-standard.

Syntax

Call R.takeLastWhile. Start with this line.

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

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

Library declarations
Ramda
R.takeLastWhile(pred, list) or R.takeLastWhile(pred)(list)

Parameters

ParameterDescription
predRequired. Called with one item; a truthy result keeps going
listRequired. Array or string — never changed

Return Value

Return ValueDescription
*A new array (or string) with the trailing run

Ramda Examples

Example 1 — Official Samples

Keep items from the end until you hit 1, then do the same on a string.

Ramda
const isNotOne = x => x !== 1;

console.log(R.takeLastWhile(isNotOne, [1, 2, 3, 4]));   // [2, 3, 4]
console.log(R.takeLastWhile(x => x !== 'R', 'Ramda')); // 'amda'
Try It Yourself

How It Works

  1. Start at the end. 4, 3, and 2 are not 1, so they are kept.
  2. Stop. 1 fails the test, so the walk ends there and 1 is excluded.
  3. Original order. The kept items come back as [2, 3, 4], not reversed.
  4. Strings. Each character is tested; everything after 'R' is kept, and the result is the string 'amda'.

Example 2 — It Is Not a Filter

Keep the trailing scores of 7 or more, and compare with R.filter.

Ramda
const scores = [9, 3, 8, 7, 10];
const atLeast7 = x => x >= 7;

console.log(R.takeLastWhile(atLeast7, scores)); // [8, 7, 10]
console.log(R.filter(atLeast7, scores));        // [9, 8, 7, 10]
Try It Yourself

How It Works

  1. From the end. 10, 7, and 8 pass.
  2. The break. 3 fails, so the walk stops — 9 is never checked.
  3. Filter checks everything. R.filter skips 3 but still keeps 9.
  4. Rule of thumb. Use takeLastWhile when only the unbroken run at the end matters.

Example 3 — Current Winning Streak

Count how many games in a row were won, ending with the latest one.

Ramda
const results = ['win', 'loss', 'win', 'win', 'win'];

const streak = R.pipe(
  R.takeLastWhile(R.equals('win')),
  R.length
);

console.log(R.takeLastWhile(R.equals('win'), results)); // ['win', 'win', 'win']
console.log(streak(results));                           // 3
console.log(streak(['win', 'win', 'loss']));            // 0
Try It Yourself

How It Works

  1. The test. R.equals('win') returns true only for 'win'.
  2. Trailing run. The last three results are wins; the 'loss' before them stops the walk.
  3. Count it. R.pipe passes the run to R.length: 3.
  4. No streak. When the latest game is a loss, the run is empty and the streak is 0.

Example 4 — Read the End of a String

Grab a file extension and a trailing order number.

Ramda
const isDigit = R.test(/\d/);

console.log(R.takeLastWhile(c => c !== '.', 'report.final.pdf')); // 'pdf'
console.log(R.takeLastWhile(isDigit, 'order-2045'));               // '2045'
console.log(R.takeLastWhile(isDigit, 'no-digits'));                // ''
Try It Yourself

How It Works

  1. Extension. Characters are kept from the end until the last dot, so only 'pdf' is returned.
  2. Digit test. R.test(/\d/) returns true for a single digit character.
  3. Trailing number. 5, 4, 0, 2 pass; the - stops the walk: '2045'.
  4. Nothing found. 's' fails right away, giving an empty string (shown as "" in the lab).

Example 5 — Unread Messages at the End

Split a chat into the read history and the unread messages at the end.

Ramda
const messages = [
  { text: 'Hi',      read: true },
  { text: 'Lunch?',  read: true },
  { text: 'Call me', read: false },
  { text: 'Urgent!', read: false }
];

const isUnread = R.propEq(false, 'read');

console.log(R.pluck('text', R.takeLastWhile(isUnread, messages))); // ['Call me', 'Urgent!']
console.log(R.pluck('text', R.dropLastWhile(isUnread, messages))); // ['Hi', 'Lunch?']
Try It Yourself

How It Works

  1. The test. R.propEq(false, 'read') is true for unread messages.
  2. Take the tail. R.takeLastWhile keeps the two unread messages at the end.
  3. The rest. R.dropLastWhile with the same test returns everything before them.
  4. Show text. R.pluck('text') pulls out just the message text for printing.

Browser & Runtime Support

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

✓ Ramda 0.16.0+ · Any JS runtime

Ramda.js R.takeLastWhile()

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.takeLastWhile() 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.takeLastWhile(pred, list) starts at the end of the list and keeps items while pred returns true. It stops at the first item that fails and returns the kept items in their original order. Official sample: R.takeLastWhile(x => x !== 1, [1, 2, 3, 4]) returns [2, 3, 4].
R.filter checks every item and keeps all that pass. R.takeLastWhile stops at the first failure from the end, so earlier passing items are not included. R.takeLastWhile(x => x >= 7, [9, 3, 8, 7, 10]) is [8, 7, 10], while R.filter gives [9, 8, 7, 10].
R.takeWhile starts at the beginning and keeps a leading run. R.takeLastWhile starts at the end and keeps a trailing run. Both stop at the first item that fails the test.
Yes. The predicate receives one character at a time and the result is a string: R.takeLastWhile(x => x !== 'R', 'Ramda') returns 'amda'.
The result is empty ([] or ''), even if earlier items would pass, because the walk stops immediately.
No. R.takeLastWhile has been a stable Ramda list function since v0.16.0. It is not deprecated, experimental, or non-standard.

Next: R.takeWhile()

Keep items from the start of a list until a test fails.

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