es-toolkit/docs/reference/array/takeRight.md
Minsoo Kim cf65b2c601
style(*): Setup prettier and apply formatting (#24)
* chore: add prettierrc

* chore: apply format with prettier config

* chore: eslint error fix
2024-06-04 17:19:26 +09:00

649 B

takeRight

Returns a new array containing the last count elements from the input array arr.

If count is greater than the length of arr, the entire array is returned.

Signature

function takeRight<T>(arr: T[], count: number): T[];

Parameters

  • arr (T[]): The array to take elements from.
  • count (number): The number of elements to take.

Returns

(T[]) A new array containing the last count elements from arr.

Examples

// Returns [4, 5]
takeRight([1, 2, 3, 4, 5], 2);

// Returns ['b', 'c']
takeRight(['a', 'b', 'c'], 2);

// Returns [1, 2, 3]
takeRight([1, 2, 3], 5);