xxxxxxxxxx
import _ from 'lodash';
let array = [ 1, 2, 3 ];
_.last(array); // 3
xxxxxxxxxx
let array = [1,2,3,4,5]
let sliced = array.slice(-1)[0]
//OR
let popped = array.slice(-1).pop()
//OR
let lengthed = array[array.length - 1]
xxxxxxxxxx
var Cars = ["Volvo", "Mazda", "Lamborghini", "Maserati"];
//We can get the total number of elements like this.
var hmCars = Cars.pop();
//hmCars is now Maserati
console.log(hmCars)
xxxxxxxxxx
const arr = [5, 3, 2, 7, 8];
const last = arr.at(-1);
console.log(last);
/*
Output: 8
*/
//Better and shorter way will be
const arr = [1,2,3,4]
console.log(arr.pop())
/*
Output: 4
*/
xxxxxxxxxx
let arry = [2, 4, 6, 8, 10, 12, 14, 16];
let lastElement = arry[arry.length - 1];
console.log(lastElement);
xxxxxxxxxx
const myArray = [1, 2, 3, 4, 5];
// Using array indexing
const lastElement = myArray[myArray.length - 1];
console.log(lastElement); // This will print 5, which is the last element of the array
xxxxxxxxxx
const colors = ['black', 'white', 'red', 'yellow'];
const yellow = colors.at(-1);
xxxxxxxxxx
let array = [0, 1, 2, 3, 4, 5, 6, 7]
console.log(array.slice(-1));
>>>[7]
console.log(array.slice(-2));
>>>[6, 7]
console.log(array.slice(-3));
>>>[5, 6, 7]