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

Lodash _.last() Method

Lodash _.last() method is used to get the last element of the array i.e. (n-1)th element. so it returns the value of the last index of an array.

Syntax:

_.last(array);

Parameters:

  • array: This function accepts a single parameter i.e. the array.

Return Value:

It returns the last element of the array.

Example 1: In this example, we are printing the value of the last index for a given array by the use of the _.last() method.

javascript




// Requiring the lodash library
const _ = require("lodash");
 
// Original array
let array1 = ["a", "b", "c", "d", "e"]
 
// Using _.last() method
let value = _.last(array1);
 
// Printing original Array
console.log("original Array1: ", array1)
 
// Printing the value
console.log("value: ", value)


Output:

Example 2: In this example, we are printing the value of the last index for a given array by the use of the _.last() method. but the array is empty so it is returning undefined.

javascript




// Requiring the lodash library
const _ = require("lodash");
 
// Original array
let array1 = []
 
// Using _.last() method
let value = _.last(array1);
 
// Printing original Array
console.log("original Array1: ", array1)
 
// Printing the value
console.log("value: ", value)


Output: 

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6625 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6694 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS