Ramda.js R.takeWhile() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.takeWhile(pred, list) keeps items from the start of a list while pred returns true, and stops at the first item that fails (official Ramda docs). The failing item is left out, and the original list is not changed.

Added in v0.1.0: R.takeWhile is stable — not deprecated, experimental, or non-standard. Note: it stops at the first failing item, so it is not a filter — later matches are ignored.

Syntax

Call R.takeWhile. Start with this line.

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

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

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

Parameters

ParameterDescription
predRequired. Gets one item; true keeps it and continues, false stops
listOptional. An array or string; omit it to get a reusable function

Return Value

Return ValueDescription
*A new array (or string) with the leading items that passed

Ramda Examples

Example 1 — Official Samples

Take numbers until a 4 appears, and letters until a 'd' appears — both samples from the Ramda docs.

Ramda
const isNotFour = x => x !== 4;

console.log(R.takeWhile(isNotFour, [1, 2, 3, 4, 3, 2, 1]));   // [1, 2, 3]
console.log(R.takeWhile(x => x !== 'd', 'Ramda'));            // 'Ram'
Try It Yourself

How It Works

  1. Numbers. 1, 2, and 3 pass; 4 fails, so the walk stops there.
  2. Left out. The 4 and the 3, 2, 1 after it are not in the result.
  3. String. 'R', 'a', 'm' pass; 'd' fails, so the result is 'Ram'.
  4. Same type. A list gives back a list and a string gives back a string.

Example 2 — Read the Header Lines

Keep the lines at the top of a file that start with #, stopping at the first normal line.

Ramda
const lines = ['# Title', '# Notes', 'Body text', '# Footer'];

const header = R.takeWhile(R.startsWith('#'), lines);

console.log(header);   // ['# Title', '# Notes']
Try It Yourself

How It Works

  1. Test. R.startsWith('#') returns true for lines beginning with #.
  2. Keep. '# Title' and '# Notes' pass.
  3. Stop. 'Body text' fails, so the walk ends.
  4. Not a filter. '# Footer' also starts with #, but it comes after the stop, so it is left out.

Example 3 — Readings Below a Limit

Keep sensor readings until the first one reaches 50, using R.lt with a placeholder.

Ramda
const readings = [10, 30, 45, 60, 20];

const isBelow50 = R.lt(R.__, 50);   // x => x < 50

console.log(R.takeWhile(isBelow50, readings));   // [10, 30, 45]
Try It Yourself

How It Works

  1. Placeholder. R.__ leaves the first slot of R.lt open, so isBelow50(x) means x < 50.
  2. Keep. 10, 30, and 45 are all below 50.
  3. Stop. 60 fails the test, so the walk ends.
  4. Ignored. The 20 after it is never checked, even though it is below 50.

Example 4 — Records Until the First Failure

Keep the jobs that succeeded before the first failed one, then list their ids.

Ramda
const jobs = [
  { id: 1, status: 'ok' },
  { id: 2, status: 'ok' },
  { id: 3, status: 'fail' },
  { id: 4, status: 'ok' }
];

const okRun = R.takeWhile(R.propEq('ok', 'status'), jobs);

console.log(R.pluck('id', okRun));   // [1, 2]
Try It Yourself

How It Works

  1. Test. R.propEq('ok', 'status') checks that a job’s status is 'ok'.
  2. Keep. Jobs 1 and 2 pass.
  3. Stop. Job 3 failed, so the walk ends; job 4 is never checked.
  4. Ids. R.pluck('id') lists the kept jobs: [1, 2].

Example 5 — takeWhile vs filter vs dropWhile vs takeLastWhile

Same test, same list — four very different results.

Ramda
const isNotFour = x => x !== 4;
const nums = [1, 2, 3, 4, 3, 2, 1];

console.log(R.takeWhile(isNotFour, nums));       // [1, 2, 3]
console.log(R.filter(isNotFour, nums));          // [1, 2, 3, 3, 2, 1]
console.log(R.dropWhile(isNotFour, nums));       // [4, 3, 2, 1]
console.log(R.takeLastWhile(isNotFour, nums));   // [3, 2, 1]
Try It Yourself

How It Works

  1. takeWhile. Keeps the leading run before the first 4.
  2. filter. R.filter checks every item and removes only the 4.
  3. dropWhile. R.dropWhile removes the same leading run and returns the rest, starting at 4.
  4. takeLastWhile. R.takeLastWhile does the same walk from the end, giving [3, 2, 1].

Browser & Runtime Support

R.takeWhile() has been in Ramda since v0.1.0. It is a plain loop plus a slice, so it works wherever Ramda loads.

✓ Ramda 0.1.0+ · Any JS runtime

Ramda.js R.takeWhile()

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

Bottom line: Safe in every Ramda version and every modern or legacy runtime. Load ramda.min.js from a CDN in the browser, or npm install ramda for Node, Deno, and Bun. These labs load [email protected].

Frequently Asked Questions

R.takeWhile(pred, list) keeps items from the start of the list while pred returns true, and stops at the first item that fails. The official sample R.takeWhile(x => x !== 4, [1, 2, 3, 4, 3, 2, 1]) returns [1, 2, 3].
No. The first failing item and everything after it are left out. In the official sample, 4 fails, so the result ends at 3.
R.filter checks every item and keeps all that pass. R.takeWhile stops for good at the first failure, so later matches are ignored. On [1, 4, 2, 3] with x => x !== 4, filter gives [1, 2, 3] but takeWhile gives [1].
R.dropWhile removes the same leading run and returns the rest. R.takeLastWhile works from the end of the list instead of the start. R.take keeps a fixed number of items without any test.
Yes. A string is checked one character at a time and the result is a string, so R.takeWhile(x => x !== 'd', 'Ramda') returns 'Ram'.
No. R.takeWhile has been part of Ramda since v0.1.0 and is stable. It is not deprecated, experimental, or non-standard.

Next: R.times()

Build a list by calling a function once for each index.

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