Skip to main content

takeRight()

takeRight<T>(array, count): T[]

Creates a slice of array with n elements taken from the end.


Type Parametersโ€‹

T: Tโ€‹

The type of elements in the array.


Parametersโ€‹

array: readonly T[]โ€‹

The array to query.

count: numberโ€‹

The number of elements to take.


Returns: T[]โ€‹

A new array with the last count elements.


Throwsโ€‹

RangeError If count is negative.


Sinceโ€‹

2.0.0


Also known asโ€‹

takeLast (Remeda, Ramda) ยท takeRight (Lodash, es-toolkit, Effect, Modern Dash) ยท โŒ (Radashi, Antfu)


Exampleโ€‹

const numbers = [1, 2, 3, 4, 5];

takeRight(numbers, 3);
// => [3, 4, 5]

takeRight(numbers, 0);
// => []

How it works?โ€‹

Takes elements from the end instead of the beginning.


Use Casesโ€‹

Show recent activity in dashboards ๐Ÿ“Œโ€‹

Display the last N actions for quick context. Perfect for "Recent Files", "Last Commands", or activity feeds.

const auditLog = [
"User login",
"File uploaded",
"Settings changed",
"Report generated",
"User logout",
];

const recentActions = takeRight(auditLog, 3);
// => ["Settings changed", "Report generated", "User logout"]

Extract trailing data points for trend analysisโ€‹

Get the most recent values for sparklines or mini-charts. Ideal for dashboards, stock tickers, or performance monitors.

const cpuHistory = [45, 52, 48, 67, 72, 68, 71, 85, 82, 79];

const recentTrend = takeRight(cpuHistory, 4);
// => [71, 85, 82, 79]

const isIncreasing = recentTrend[3] > recentTrend[0];
// => true (trending up)

Get last page of paginated dataโ€‹

Extract the final batch when you know total count. Useful for "Jump to last page" functionality.

const allItems = Array.from({ length: 47 }, (_, i) => `Item ${i + 1}`);
const pageSize = 10;

const lastPage = takeRight(allItems, 47 % pageSize || pageSize);
// => ["Item 41", "Item 42", ..., "Item 47"]