How Lodash takeRight Extracts Array Elements
The Lodash _.takeRight method creates a slice of an
array with a specified number of elements taken from the end, leaving
the original array unaltered. This article explains how
_.takeRight works under the hood, details its syntax and
default parameters, demonstrates its behavior through code examples, and
examines how it handles edge cases compared to native JavaScript
alternatives.
Syntax and Parameters
The syntax for _.takeRight is concise:
_.takeRight(array, [n=1])array: The array to query.n(optional): The number of elements to extract from the end. The default value is1.
The method returns a completely new array containing the extracted slice.
How the Extraction Works
Internally, _.takeRight determines which elements to
retrieve by calculating a starting offset based on the array's total
length and the requested number n.
- Length Evaluation: It checks the
lengthof the provided collection. If the collection is null, undefined, or empty, it immediately returns an empty array ([]). - Offset Calculation: The starting point for the
slice is computed using the equivalent of
Math.max(length - n, 0). - Element Slicing: Once the starting index is
established, Lodash extracts all items from that index up to the end of
the array using an internal non-mutating slice implementation
(
baseSlice).
Because it creates a shallow copy, elements in the newly created array share references with original objects, but adding or removing elements from the new array does not affect the source array.
Practical Examples
Default Extraction
When the second argument n is omitted,
_.takeRight defaults to taking just the last element:
const numbers = [10, 20, 30, 40, 50];
const result = _.takeRight(numbers);
console.log(result);
// Output: [50]Specifying Count
Passing an integer allows extraction of multiple items from the end:
const fruits = ['apple', 'banana', 'cherry', 'date'];
const lastTwo = _.takeRight(fruits, 2);
console.log(lastTwo);
// Output: ['cherry', 'date']Handling Edge Cases
_.takeRight includes built-in safeguards for boundary
inputs:
- Zero or Negative
n: Ifn <= 0, the method returns an empty array ([]). nExceeds Array Length: Ifnis greater than or equal to the array length, a shallow copy of the entire array is returned without throwing an error.- Non-Array Values: If passed
nullorundefined, it safely evaluates to[].
const items = ['a', 'b', 'c'];
console.log(_.takeRight(items, 0)); // []
console.log(_.takeRight(items, -2)); // []
console.log(_.takeRight(items, 10)); // ['a', 'b', 'c']
console.log(_.takeRight(null, 2)); // []Comparison with Native JavaScript
In modern JavaScript, the native equivalent to
_.takeRight(array, n) is array.slice(-n).
However, native Array.prototype.slice behaves differently
when n is zero (returning the whole array instead of an
empty one) and will throw a TypeError if invoked on
null or undefined. Lodash's
_.takeRight abstracts these checks, guaranteeing consistent
array output across all inputs.