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 colors = ["red","blue","green"];
var green = colors[colors.length - 1]; //get last item in the array
xxxxxxxxxx
var colors = ["red","blue","green"];
var green = colors[colors.length - 1]; //get last item in the array
xxxxxxxxxx
let nums = [1,2,3,4,5];
let lastOne = nums.pop();
// -> lastOne = 5
// -> nums = [1,2,3,4];
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