how to get last item of array in javascript and compare with current code example
Example 1: javascript last element of an array
var colors = ["red","blue","green"];
var green = colors[colors.length - 1];//get last
Example 2: last item in array javascript
const array = ['a', 's', 'd', 'f'];
const last = array.pop();
console.log(last); // Returns 'f'
console.log(array); // Returns ['a', 's', 'd']