Ramda.js R.count() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.count(pred, list) returns how many items in list make the predicate pred return a truthy value (official Ramda docs). You get a single number back, and the list is never changed.

Added in v0.28.0: R.count is stable — not deprecated, experimental, or non-standard. Note: it does not exist in older Ramda versions, so check your version before upgrading code that uses it.

Syntax

Call R.count. Start with this line.

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

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

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

Parameters

ParameterDescription
predRequired. Function called with each item (no index); truthy means "count it"
listOptional. Array (or other iterable) to check; omit it to get a reusable counter

Return Value

Return ValueDescription
*A number from 0 to the list length

Ramda Examples

Example 1 — Official Count Even Numbers

Count the even numbers in one list, then in each list of a nested list — the samples from the Ramda docs.

Ramda
const even = x => x % 2 == 0;

console.log(R.count(even, [1, 2, 3, 4, 5]));                     // 2
console.log(R.map(R.count(even), [[1, 1, 1], [2, 3, 4, 5], [6]]));  // [0, 2, 1]
Try It Yourself

How It Works

  1. Predicate. even returns true for numbers with no remainder when divided by 2.
  2. Count. Only 2 and 4 pass, so the first call returns 2.
  3. Curried. R.count(even) has no list yet, so it returns a counter function.
  4. Map. R.map runs that counter on each inner list: 0, 2, and 1 evens.

Example 2 — Count Matching Objects

Count records by a property value or a property test, without writing a custom function.

Ramda
const users = [
  { name: 'Ana', role: 'admin', age: 34 },
  { name: 'Ben', role: 'user', age: 17 },
  { name: 'Cy', role: 'admin', age: 15 },
  { name: 'Di', role: 'user', age: 41 }
];

const isAdmin = R.propEq('admin', 'role');
const isAdult = R.propSatisfies(age => age >= 18, 'age');

console.log(R.count(isAdmin, users));  // 2
console.log(R.count(isAdult, users));  // 2
Try It Yourself

How It Works

  1. Equal value. R.propEq('admin', 'role') checks user.role === 'admin' (value first in Ramda 0.29+).
  2. Custom test. R.propSatisfies runs age => age >= 18 on each user’s age.
  3. Count admins. Ana and Cy pass, so the first result is 2.
  4. Count adults. Ana (34) and Di (41) pass, so the second result is also 2.

Example 3 — Build Reusable Counters

Give R.count only a predicate to get a named counter, and flip the test with R.complement.

Ramda
const even = x => x % 2 == 0;

const countEven = R.count(even);
const countOdd = R.count(R.complement(even));

console.log(countEven([1, 2, 3, 4, 5]));  // 2
console.log(countOdd([1, 2, 3, 4, 5]));   // 3
Try It Yourself

How It Works

  1. Partial call. R.count(even) returns a function that waits for a list.
  2. Opposite test. R.complement(even) returns true wherever even returns false.
  3. Run them. 2, 4 are even (2 items); 1, 3, 5 are odd (3 items).
  4. Check. 2 + 3 = 5, the full list length — every item is counted by exactly one counter.

Example 4 — Count Truthy and Missing Values

Any truthy return value counts, so built-ins like Boolean and R.isNil work as predicates.

Ramda
const values = [0, 1, '', 'a', null, 2];

console.log(R.count(Boolean, values));   // 3
console.log(R.count(R.isNil, values));   // 1
Try It Yourself

How It Works

  1. Truthy. Boolean(x) is true for 1, 'a', and 2; 0, '', and null are falsy.
  2. Count truthy. Three values pass, so the first result is 3.
  3. Missing values. R.isNil is true only for null and undefined.
  4. Count missing. Only null passes, so the second result is 1 — 0 and '' are not nil.

Example 5 — count vs filter vs length

Three ways to get a number from a list — pick the one that matches your question.

Ramda
const even = x => x % 2 == 0;
const nums = [1, 2, 3, 4, 5];

console.log(R.count(even, nums));          // 2
console.log(R.filter(even, nums).length);  // 2
console.log(R.length(nums));               // 5
console.log(R.count(even, []));            // 0
Try It Yourself

How It Works

  1. count. R.count answers “how many match?” directly: 2.
  2. filter + length. R.filter builds [2, 4] first, then .length reads 2 — same answer, extra array.
  3. length. R.length ignores any test and returns all 5 items.
  4. Empty list. Nothing to test, so R.count safely returns 0.

Browser & Runtime Support

R.count() is plain JavaScript inside Ramda 0.28.0+. It is a simple reduce with a counter, so it works wherever Ramda loads.

✓ Ramda 0.28.0+ · Any JS runtime

Ramda.js R.count()

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% With Ramda 0.28+
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.count() Universal

Bottom line: Safe in any project on Ramda 0.28 or newer; older versions have no R.count. 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.count(pred, list) returns how many items in the list make the predicate return a truthy value. The result is a number, not a list. The official sample is R.count(even, [1, 2, 3, 4, 5]), which returns 2.
R.filter returns the matching items; R.count returns only how many matched. R.filter(pred, list).length gives the same number, but it builds an extra array first.
R.length returns the size of the whole list and takes no predicate. R.countBy(fn, list) returns an object with a count per key, such as { false: 3, true: 2 }, instead of one number.
No. R.count calls the predicate with the item only. If you need the index, use the native list.filter((x, i) => ...).length or Array.prototype.reduce.
It returns 0 for an empty list, and also 0 for null or undefined. Plain objects throw 'reduce: list must be array or iterable'; count their values with R.count(pred, R.values(obj)).
No. R.count was added in Ramda v0.28.0 and is stable. It is not deprecated, experimental, or non-standard, but it does not exist in Ramda versions older than 0.28.

Next: R.drop()

Skip the first n items of a list or string.

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