Open In App

Lodash _.last() Method

Improve
Improve
Like Article
Like
Save
Share
Report

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: 


Last Updated : 20 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads