Lodash _.nth() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.nth(array, n) reads one slot by index without mutating the collection.
  • That negative n counts from the end (for example -1 behaves like _.last() on a dense array).
  • How out-of-range indexes yield undefined, and how this compares to array[i] and at.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Helpful context: _.head() (index 0), _.last() (tail slot), and _.lastIndexOf() when you need an index instead of a value.

  • You understand zero-based indexing and that missing slots read as undefined.
  • You can run snippets in Node or open the Try-it labs in a browser.

Overview

_.nth is the small, explicit reader for “element at position n.” Positive indexes walk from the start; negative indexes walk from the end. It never allocates a new array and returns undefined when the resolved index is outside the collection.

Any offset

One helper covers middle reads as well as ends, unlike fixed head / last helpers.

Negative n

Use -1 for the last element, -2 for the penultimate, and so on.

Importable

Use lodash/nth beside other per-method imports in a tree-shaken bundle.

Syntax

javascript
_.nth(array, n)
  • array: collection to read (array-like values are coerced).
  • n: zero-based index, or a negative offset from the end of the collection.
  • Returns: the element at the resolved index, or undefined when the index is out of range.
1

Positive index

Non-negative n selects the same slot as bracket notation on a dense array.

javascript
import nth from "lodash/nth";

nth(["a", "b", "c"], 1);
// "b"
Try it Yourself
2

Negative index

-1 resolves to the last element when the array is non-empty—handy when you do not want to write length - 1.

javascript
import nth from "lodash/nth";

nth([10, 20, 30], -1);
// 30
Try it Yourself
3

Out of range

Indexes at or beyond length (or before the start for negatives) return undefined instead of throwing.

javascript
import nth from "lodash/nth";

nth([1, 2], 5);
// undefined
Try it Yourself

📋 _.nth vs array[i] vs at()

APIStrengthNote
_.nth(array, n)Named intent, negative offsets, array-likeMatches other Lodash per-method imports
array[i]Fast bracket readNo built-in negative index; guard i against length
array.at(n)Native relative indexingES2022; similar ergonomics for negative n on real arrays

Pitfalls to avoid

Sparse

Empty slots

A hole at the resolved index still yields undefined; distinguish “missing slot” from “out of range” when debugging.

n

Non-integer n

Lodash coerces n toward an integer; prefer passing whole indexes when the value comes from user input or math.

0

Falsy elements

0, "", and false are valid elements; do not treat them as “no result” after calling nth.

❓ FAQ

No. It resolves the requested index and returns that element reference without changing length or order.
It returns undefined for positions before the start or at/after the length of the collection.
They count backward from the end: -1 is the last element, -2 the second-to-last, mirroring Array.prototype.at-style ergonomics.
head and last are fixed endpoints. nth generalizes to any offset, including negatives, in one helper.
Use import nth from "lodash/nth" or require("lodash/nth") for tree-shaking friendly bundles.

Summary

Did you know?

Strings are array-like to nth as well: nth("hello", 1) yields "e"—the same character you would read with bracket syntax.

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