Thursday, September 4, 2025
HomeLanguagesJavascriptLodash _.takeRight() Method

Lodash _.takeRight() Method

The _.takeRight() method is used to get the array of n elements from the end of the given array.

Syntax:

_.takeRight(array, n)

Parameters: This method accept two parameters as mentioned above and described below:

  • array: This parameter holds the query array.
  • n: This parameter holds the number of element.

Return Value: This method returns an array containing n elements from the given end.

Example 1:




const _ = require('lodash');
  
let x = [1, 2, 3, 4, 5, 6, 7]
  
let newArray = _.takeRight(x);
  
console.log(newArray);


Here, const _ = require('lodash') is used to import the lodash library into the file.

Output:

[7]

Example 2:




const _ = require('lodash');
  
let x = [1, 2, 3, 4, 5, 6, 7]
  
let newArray = _.takeRight(x, 5);
  
console.log(newArray);


Output:

[ 3, 4, 5, 6, 7 ]

Example 3:




const _ = require('lodash');
  
let x = [1, 2, 3, 4, 5, 6, 7]
  
let newArray = _.takeRight(x, 0);
  
console.log(newArray);


Output:

[]

Note: This will not work in normal JavaScript because it requires the library lodash to be installed.

Reference: https://lodash.com/docs/4.17.15#takeRight

RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6629 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11858 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS