Lodash _.takeRight() method

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

What you’ll learn

  • How _.takeRight(array, n) clones the trailing suffix without mutating the source.
  • That omitting n keeps exactly one tail element and n = 0 yields [].
  • When negative _.slice() offsets or predicate helpers express the intent clearer.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Read _.take() first—the APIs differ only in which end they measure from.

  • You understand shallow copies: nested values inside the suffix remain aliased.
  • You can open Try-it labs or run snippets locally.

Overview

_.takeRight captures trailing crumbs—recent audit entries, final pagination page, or suffix tokens—without recomputing lengths manually.

Suffix-only

Anchors at the highest indexes—ideal complements to leading-edge helpers.

Count clamping

Oversized counts snap to the full span instead of throwing.

Non-destructive

Earlier cells remain available for alternate projections.

Syntax

javascript
_.takeRight(array, [n=1])
  • array: dense collection or array-like value.
  • n: how many trailing elements to include (defaults to 1).
  • Returns: new array with at most n slots copied from the end.
1

Grab the closing run

Order inside the suffix mirrors the original sequence ending at the last index.

javascript
import takeRight from "lodash/takeRight";

takeRight(["spring", "summer", "autumn", "winter"], 2);
// → ["autumn", "winter"]
Try it Yourself
2

Zero count vs default

n = 0 produces []; omitting n captures only the final element.

javascript
import takeRight from "lodash/takeRight";

takeRight(["east", "south", "west"], 0);
// → []

takeRight(["east", "south", "west"]);
// → ["west"]
Try it Yourself
3

Count larger than length

Huge counts normalize to a whole-array shallow duplicate.

javascript
import takeRight from "lodash/takeRight";

takeRight([7, 8, 9], 100);
// → [7, 8, 9]
Try it Yourself

📋 _.takeRight vs _.take, _.slice, _.dropRight

APIEdgeUse case
_.takeRight(array, n)TrailingKnown suffix widths such as recent windows
_.take(array, n)LeadingPrefixes instead of suffixes
_.slice(array, -n)Trailing via negative startNative-style slicing when Lodash import unnecessary
_.dropRight(array, n)Removes trailing cellsKeep the complementary head segment

Pitfalls to avoid

Deep

Shallow suffix

Nested structures remain referenced—clone deeply only when required.

Mix

initial confusion

_.initial() drops exactly one tail slot but returns the complementary prefix; takeRight(_, 1) keeps only the last element.

Sparse

Sparse arrays

Holes may appear depending on engines—prefer dense arrays for deterministic QA.

❓ FAQ

No. Lodash returns a new array containing up to n trailing elements; the source stays unchanged.
Lodash defaults n to 1, so _.takeRight(array) keeps only the final slot.
You receive a shallow copy spanning the entire dense collection—same clamping idea as _.take.
take reads from index zero forward; takeRight walks inward from the last index.
Use import takeRight from "lodash/takeRight" or require("lodash/takeRight") for tree-shaking friendly bundles.

Summary

Did you know?

_.takeRight pairs naturally with _.dropRight() the same way _.take() pairs with _.drop()—mirror trims from opposite ends of the buffer.

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