0
Q:

find last item in an array JS

var heroes = ["Batman", "Superman", "Hulk"];
var lastHero = heroes.pop(); // Returns last elment of the Array
// lastHero = "Hulk"
5
var colors = ["red","blue","green"];
var green = colors[colors.length - 1]; //get last item in the array
8
arr.slice(-1)[0] 
1
var colors = ["black", "white", "red", "yellow"];
var yellow = colors[colors.length - 1];
1
const numArray = [1,2,3,4,5];
const lastNumber = numArray.reverse()[0]; 
1
if (loc_array[loc_array.length - 1] === 'index.html') {
   // do something
} else {
   // something else
}
1
var a = loc_array.slice(-1)[0]
1
get last item of array javascript
0
var my_array = /* some array here */;
var last_element = my_array[my_array.length - 1];
0

New to Communities?

Join the community