xxxxxxxxxx
// how to find last element in array in javascript
// 1. Array.prototype.length
const arr = [1,2,3,4,5];
console.log(arr[arr.length - 1]);
// Result: 5
// 2. Array.prototype.slice()
const last = arr.slice(-1);
console.log(+last);
// Result: 5
xxxxxxxxxx
var colors = ["red","blue","green"];
var green = colors[colors.length - 1]; //get last item in the array
xxxxxxxxxx
var heroes = ["Batman", "Superman", "Hulk"];
var lastHero = heroes.pop(); // Returns last elment of the Array
// lastHero = "Hulk"
xxxxxxxxxx
let nums = [1,2,3,4,5];
let lastOne = nums.pop();
// -> lastOne = 5
// -> nums = [1,2,3,4];
xxxxxxxxxx
const colors = ['black', 'white', 'red', 'yellow'];
const yellow = colors.at(-1);