GettingRetrieving the last item in an array is possible via the length property. Since the array count starts at 0, you can pick the last item by referencing the array.length - 1 item
const arr = [1,2,3,4]; const last = arr[arr.length - 1]; console.log(last); // 4 Another option is using the new Array.prototype.at() method which takes an integer value and returns the item at that index. Negative integers count back from the last item in the array so if we want the last item we can just pass in -1
const arr = [1,2,3,4]; const last = arr.at(-1); console.log(last); // 4 Another option is using the new findLast method. You can see the proposal here (currently in stage 4)
const arr = [1,2,3,4]; const last = arr.findLast(x => true); console.log(last); // 4 Another option is using the Array.prototype.slice() method which simply returns a shallow copy of a portion of an array into a new array object.
const arr = [1,2,3,4]; const last = arr.slice(-1)[0]; console.log(last); // 4