JavaScript obtenga el último elemento de matriz
var colors = ["red","blue","green"];
var green = colors[colors.length - 1];//get last item in the array
Friendly Hawk
var colors = ["red","blue","green"];
var green = colors[colors.length - 1];//get last item in the array
const heroes = ["Batman", "Superman", "Hulk"];
const lastHero = heroes.pop(); // Returns last elment of the Array
// lastHero = "Hulk"
let nums = [1,2,3,4,5];
let lastOne = nums.pop();
// -> lastOne = 5
// -> nums = [1,2,3,4];
var arr = [1, 2, 3];
var last_element = arr.reverse()[0];
// 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
if (locArray.at(-1) === 'index.html') {
// do something
} else {
// something else
}