Skip to content
Related Articles

Related Articles

Improve Article

Lodash _.last() Method

  • Last Updated : 22 Jul, 2020

Lodash is a JavaScript library that works on the top of Underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. The _.last() method is used to get the last element of the array i.e. (n-1)th element.

Syntax:

lodash.last( array )

Parameters: This function accepts single parameter i.e. the array.

Return Value: It returns the last element of the array.

Note: Please install lodash module by npm install lodash before using the below given code.



Example 1:

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:

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: 

My Personal Notes arrow_drop_up
Recommended Articles
Page :