Q:

javascript code for find the last element in array

var heroes = ["Batman", "Superman", "Hulk"];
var lastHero = heroes.pop(); // Returns last elment of the Array
// lastHero = "Hulk"
5
arr.slice(-1)[0] 
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
const myArray = [1, 2, 3]

console.log(myArray.item(-1))
//=> 3
0
var my_array = /* some array here */;
var last_element = my_array[my_array.length - 1];
0

New to Communities?

Join the community