Lodash _.nth() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Gets the element at index n of array. If n is negative, the nth element from the end is returned. Part of Lodash 4.x Array (since 4.11.0).

Note: Native Array.prototype.at() (ES2022) offers the same relative-index idea. Prefer Lodash when you already ship Lodash or need older runtimes without at.

Syntax

Call _.nth on an array. Start with this line.

Lodash
_.nth(array, [n=0])
CallWhat it does
_.nth(array, [n=0])Primary signature.
_.nth(array)Call with an array argument.

Prefer import nth from "lodash/nth" when you want a smaller bundle.

Library declarations
Lodash
_.nth(array, [n=0])

Parameters

ParameterDescription
arrayRequired. Source to read from
nOptional. Index; default 0; negative from end

Return Value

Return ValueDescription
*Element, or undefined

Lodash Examples

Example 1 — Official: Positive Index

Read the element at index 1 (second item).

Lodash
_.nth(["a", "b", "c", "d"], 1);
// → "b"
Try It Yourself

How It Works

  1. Source. Four letters; indexes run 0 … 3.
  2. Ask for 1. Second slot → "b".
  3. Same idea as brackets. For non-negative indexes, like arr[1].
  4. No side effects. The array is unchanged.

Example 2 — Official: Negative Index

Count from the end — -2 is the second-to-last item.

Lodash
_.nth(["a", "b", "c", "d"], -2);
// → "c"
Try It Yourself

How It Works

  1. From the end. Negative indexes add length first.
  2. Resolve. -2 + 4 = 2 → "c".
  3. Why it helps. You avoid writing arr[arr.length - 2].
  4. Rule. -1 is last, -2 is one before last.

Example 3 — Default Index 0

Omit n and Lodash reads the first element.

Lodash
_.nth(["a", "b", "c", "d"]);
// → "a"
Try It Yourself

How It Works

  1. Default. Missing n means 0.
  2. First slot. Returns "a".
  3. Same as head. _.head(arr) is clearer when you only want the first item.
  4. Empty? _.nth([]) → undefined.

Example 4 — Out of Range

Indexes past the end (or before the start) yield undefined.

Lodash
_.nth(["a", "b", "c", "d"], 5);
// → undefined

_.nth(["a", "b", "c", "d"], -10);
// → undefined
Try It Yourself

How It Works

  1. Too large. 5 >= 4 → undefined.
  2. Too negative. -10 + 4 = -6 still before 0 → undefined.
  3. No throw. Safe for optional indexes — check the result.
  4. Caveat. A real stored undefined looks the same; compare indexes if you must tell them apart.

Example 5 — Last Item with -1

Prefer _.nth(arr, -1) or _.last(arr) for the final element.

Lodash
var letters = ["a", "b", "c", "d"];

_.nth(letters, -1);   // → "d"
_.last(letters);      // → "d"
Try It Yourself

How It Works

  1. Resolve. -1 + 4 = 3 → "d".
  2. Same result. _.last is specialized for this case.
  3. Choose. Use last/head for endpoints; nth when the offset varies.
  4. Avoid. arr[-1] is not relative indexing in JavaScript brackets.

Browser & Runtime Support

_.nth is a Lodash 4.x Array helper (since 4.11.0). It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 4.x

_.nth()

Available in [email protected] full builds and the lodash/nth modular import. Native Array.prototype.at needs ES2022 (Chrome 92+, Firefox 90+, Safari 15.4+, Node 16.6+).

100% With Lodash 4.x
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
_.nth Lodash 4.x+

Bottom line: Safe for production once Lodash is loaded. Prefer import nth from "lodash/nth" for smaller bundles. On modern-only targets, arr.at(n) is a fine native alternative.

Frequently Asked Questions

_.nth(array, n) returns the element at index n. Positive indexes count from the start (default n is 0). Negative indexes count from the end (−1 is last). Out of range returns undefined.
No. It only reads and returns the element at the resolved index. The array length and order stay the same.
It returns undefined when the resolved index is before 0 or at/after length — it never throws.
head is always the first element; last is always the last. nth generalizes both with any offset, including negatives.
Same idea for arrays: relative indexing with negatives. Native at() is ES2022. Lodash nth helps when you already use Lodash or need older runtimes.
Use import nth from "lodash/nth" or require("lodash/nth") for tree-shaking friendly bundles. Full build: import _ from "lodash" then _.nth.

Next: _.pull()

Remove all given values from an array in place.

_.pull() 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.

10 people found this page helpful