Skip to content
Related Articles
Open in App
Not now

Related Articles

Lodash _.last() Method

Improve Article
Save Article
Like Article
  • Last Updated : 22 Jul, 2020
Improve Article
Save Article
Like Article

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
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!