Lodash _.nth() method
What you’ll learn
- How
_.nth(array, n)reads one slot by index without mutating the collection. - That negative
ncounts from the end (for example-1behaves like _.last() on a dense array). - How out-of-range indexes yield
undefined, and how this compares toarray[i]andat. - 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
_.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
undefinedwhen the index is out of range.
Positive index
Non-negative n selects the same slot as bracket notation on a dense array.
import nth from "lodash/nth";
nth(["a", "b", "c"], 1);
// "b" Negative index
-1 resolves to the last element when the array is non-empty—handy when you do not want to write length - 1.
import nth from "lodash/nth";
nth([10, 20, 30], -1);
// 30 Out of range
Indexes at or beyond length (or before the start for negatives) return undefined instead of throwing.
import nth from "lodash/nth";
nth([1, 2], 5);
// undefined 📋 _.nth vs array[i] vs at()
| API | Strength | Note |
|---|---|---|
_.nth(array, n) | Named intent, negative offsets, array-like | Matches other Lodash per-method imports |
array[i] | Fast bracket read | No built-in negative index; guard i against length |
array.at(n) | Native relative indexing | ES2022; similar ergonomics for negative n on real arrays |
Pitfalls to avoid
Empty slots
A hole at the resolved index still yields undefined; distinguish “missing slot” from “out of range” when debugging.
Non-integer n
Lodash coerces n toward an integer; prefer passing whole indexes when the value comes from user input or math.
Falsy elements
0, "", and false are valid elements; do not treat them as “no result” after calling nth.
❓ FAQ
Summary
- Purpose:
_.nth(array, n)returns the element at indexn, counting backward whennis negative, orundefinedwhen out of range. - Pairs with: _.head(), _.last(), and native
atfor similar reads at fixed or relative positions. - Next: Lodash _.pull(), _.lastIndexOf() (earlier in this track), or the array methods hub.
Strings are array-like to nth as well: nth("hello", 1) yields "e"—the same character you would read with bracket syntax.
6 people found this page helpful
