Ramda.js R.nth() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

A list function that gets one item by its index. R.nth(index, list) returns the item at that position, and a negative index counts from the end (-1 is the last item). It works on arrays and strings, and it is Ramda’s curried twin of native Array.prototype.at().

Status: stable since Ramda v0.1.0 — not deprecated, experimental, or non-standard.

Syntax

Call R.nth. Start with this line.

Ramda
R.nth(index, list)
CallWhat it does
R.nth(index, list)Primary signature.
R.nth(...)Call the list helper.

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

Library declarations
Ramda
R.nth(index, list)

Parameters

ParameterDescription
indexRequired. Whole number; negative counts from the end
listRequired. An array or a string (never changed)

Return Value

Return ValueDescription
*The item at that index, or undefined

Ramda Examples

Example 1 — Official: Second, Last, and Out of Range

The three array samples from the Ramda docs.

Ramda
const list = ["foo", "bar", "baz", "quux"];

console.log(R.nth(1, list));    // → bar
console.log(R.nth(-1, list));   // → quux
console.log(R.nth(-99, list));  // → undefined
Try It Yourself

How It Works

  1. Index 1. Indexes start at 0, so 1 is the second item, "bar".
  2. Index -1. Becomes 4 - 1 = 3, the last slot, "quux".
  3. Index -99. Becomes -95; nothing lives there, so you get undefined.

Example 2 — Pick a Character From a String

A string works like a list of characters.

Ramda
console.log(R.nth(2, "abc"));     // → c
console.log(R.nth(-1, "Ramda"));  // → a
Try It Yourself

How It Works

  1. Index 2. In "abc", a is 0, b is 1, c is 2.
  2. Index -1. "Ramda" has 5 characters, so -1 becomes index 4.
  3. Result. Each call returns a one-character string.

Example 3 — A Safe Default for Missing Items

Replace undefined with a friendly fallback.

Ramda
const colors = ["red", "green", "blue"];
const pick = i => R.defaultTo("unknown", R.nth(i, colors));

console.log(pick(1));   // → green
console.log(pick(5));   // → unknown
console.log(pick(-1));  // → blue
Try It Yourself

How It Works

  1. Read. R.nth(i, colors) returns the color or undefined.
  2. Fallback. R.defaultTo swaps undefined for "unknown".
  3. Result. Index 5 doesn’t exist, so only that call prints the fallback.

Example 4 — Grab the First Column of Rows

R.nth is curried, so give it the index now and each row later.

Ramda
const rows = [["Ada", 36], ["Alan", 41], ["Grace", 85]];

console.log(R.map(R.nth(0), rows));
// → ["Ada", "Alan", "Grace"]
Try It Yourself

How It Works

  1. Partial call. R.nth(0) returns a function still waiting for a list.
  2. Map. R.map calls it once per row.
  3. Result. Each row gives back its first item, the name.

Example 5 — Get the Last Word in a Pipe

Split a full name into words, then keep the last one.

Ramda
const lastName = R.pipe(R.split(" "), R.nth(-1));

console.log(lastName("Ada King Lovelace"));  // → Lovelace
console.log(lastName("Alan Turing"));        // → Turing
Try It Yourself

How It Works

  1. Split. R.split(" ") turns the name into an array of words.
  2. Pick. R.nth(-1) takes the last word, whatever the length.
  3. Pipe. R.pipe runs both steps left to right.

Browser & Runtime Support

R.nth has been in Ramda since v0.1.0. Ramda’s browser build is plain ES5, so it runs in any modern browser and in Node.js / Bun / Deno with npm install ramda.

✓ Stable · Ramda 0.1.0+

R.nth()

Just a length check and a bracket lookup, so it even works where native Array.prototype.at() is missing, such as very old browsers.

100% With 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.nth Ramda 0.1.0+

Bottom line: Safe for production with any Ramda release. Only the out-of-range string result changed in 0.31. The labs on this page use [email protected].

Frequently Asked Questions

R.nth(index, list) returns the item at that index. Negative indexes count from the end, so -1 is the last item. Docs sample: R.nth(1, ['foo', 'bar', 'baz', 'quux']) //=> 'bar'.
undefined for arrays, and no error is thrown. R.nth(-99, ['foo', 'bar', 'baz', 'quux']) is undefined. Wrap it in R.defaultTo if you need a fallback value.
Yes. R.nth(2, 'abc') is 'c'. Out of range, Ramda 0.31 and newer return undefined, while 0.30.x and older return an empty string ''.
R.head gives the first item and R.last gives the last one. R.nth can pick any position: R.nth(0, list) is the same as R.head(list), and R.nth(-1, list) is the same as R.last(list).
They pick the same item for whole-number indexes, including negative ones. R.nth is curried with the index first, so R.nth(-1) is a reusable 'get last' function you can pass to R.map or R.pipe.
No. R.nth has been a stable Ramda list function since v0.1.0 — not deprecated, experimental, or non-standard.

Next: R.pair()

Learn how to wrap two values into a two-item array.

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