Ramda.js R.length() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.length(list) returns how many items a list has by reading its length property (official Ramda docs). It gives the same number as native Array.prototype.length, but as a function you can pass around — and it returns NaN instead of throwing when the value is null or has no numeric length.

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

Syntax

Call R.length. Start with this line.

Ramda
R.length(list)
CallWhat it does
R.length(list)Primary signature.
R.length(...)Call the list helper.

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

Library declarations
Ramda
R.length(list)

Parameters

ParameterDescription
listRequired. Array, string, function, or any value with a numeric length — never changed

Return Value

Return ValueDescription
*The length value, or NaN

Ramda Examples

Example 1 — Official Samples

Count an empty list and a list of three numbers.

Ramda
console.log(R.length([]));        // 0
console.log(R.length([1, 2, 3])); // 3
Try It Yourself

How It Works

  1. Not null. Both values are real arrays, so Ramda moves on to the second check.
  2. Numeric length. [].length is 0 and [1, 2, 3].length is 3 — both numbers.
  3. Return it. Ramda hands back that number unchanged. The arrays are not modified.

Example 2 — Strings and Emoji

Strings have a numeric length too — but it counts UTF-16 code units, not visible characters.

Ramda
console.log(R.length('ramda')); // 5
console.log(R.length(''));      // 0
console.log(R.length('😀'));    // 2
console.log([...'😀'].length);  // 1
Try It Yourself

How It Works

  1. Same rule. A string’s length is a number, so Ramda returns it.
  2. Empty string. '' has length 0, not NaN.
  3. The emoji surprise. '😀' is stored as two UTF-16 code units, so its length is 2.
  4. Count characters. Spreading [...'😀'] splits by real characters, giving 1.

Example 3 — Count in a Pipe

Count active users by filtering first, then measuring — and compare with R.count.

Ramda
const users = [
  { name: 'Ann', active: true },
  { name: 'Bob', active: false },
  { name: 'Cy',  active: true }
];

const isActive = R.propEq(true, 'active');
const countActive = R.pipe(R.filter(isActive), R.length);

console.log(countActive(users));          // 2
console.log(R.count(isActive, users));    // 2
console.log(R.length(users));             // 3
Try It Yourself

How It Works

  1. The test. R.propEq(true, 'active') checks whether active is true.
  2. Filter, then measure. R.pipe runs R.filter (keeps Ann and Cy), then R.length counts them: 2.
  3. No wrapper needed. R.length is already a one-argument function, so it slots straight into the pipe.
  4. Shortcut. R.count gives the same 2 without building the filtered array. R.length(users) counts everyone: 3.

Example 4 — Missing Data and a Safe Default

Missing values give NaN, not an error. Turn that into 0 with R.defaultTo.

Ramda
console.log(R.length(null));      // NaN
console.log(R.length(undefined)); // NaN
console.log(R.length({}));        // NaN

const sizeOr0 = R.pipe(R.length, R.defaultTo(0));

console.log(sizeOr0(null));       // 0
console.log(sizeOr0(['a', 'b'])); // 2
Try It Yourself

How It Works

  1. No crash. null and undefined fail the first check, so Ramda returns NaN without reading .length.
  2. No length. {} has no length property, so the second check fails: NaN.
  3. Fallback. R.defaultTo(0) replaces null, undefined, and NaN with 0.
  4. Real data passes through. ['a', 'b'] has length 2, which defaultTo leaves alone.

Example 5 — Function Arity

A function’s length is how many parameters it declares — including Ramda’s own curried functions.

Ramda
console.log(R.length((a, b, c) => a)); // 3
console.log(R.length(R.add));           // 2
console.log(R.length(R.add(1)));        // 1
console.log(R.length((a, b = 2) => a)); // 1
Try It Yourself

How It Works

  1. Functions have a length. It is the number of declared parameters, so (a, b, c) => a gives 3.
  2. Curried arity. R.add takes two arguments: 2. After R.add(1), one argument is still missing: 1.
  3. Defaults stop the count. Parameters from the first default value onward are not counted, so (a, b = 2) => a gives 1.

Browser & Runtime Support

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

✓ Ramda 0.3.0+ · Any JS runtime

Ramda.js R.length()

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 and spread syntax 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.length() 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.length(list) returns the number of items in a list by reading its length property. Official samples: R.length([]) is 0 and R.length([1, 2, 3]) is 3. It also works on strings, functions, and any value whose length is a number.
They return the same number for arrays and strings. The differences: R.length is a function, so it can be passed to R.pipe or R.map, and it returns NaN for null, undefined, or values without a numeric length, where null.length throws a TypeError.
R.length returns NaN when the value is null or undefined, or when its length property is missing or not a number, such as {}, 42, a Map, or a Set. Use R.defaultTo(0) or Number.isNaN to handle it.
R.length counts every item. R.count(pred, list) counts only the items that pass a test, so R.count(isEven, [1, 2, 3, 4]) is 2 while R.length([1, 2, 3, 4]) is 4.
JavaScript strings measure length in UTF-16 code units, and many emoji use two of them. R.length('😀') is 2. To count visible characters, use [...str].length, which is 1.
No. R.length has been a stable Ramda list function since v0.3.0. It is not deprecated, experimental, or non-standard.

Next: R.map()

Transform every item in a list into something new.

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