get last element of array javascript

JavaScript
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"var heroes = ["Batman", "Superman", "Hulk"];
var lastHero = heroes.pop(); // Returns last elment of the Array
// lastHero = "Hulk"if (loc_array[loc_array.length - 1] === 'index.html') {
   // do something
} else {
   // something else
}let array = [1,2,3,4,5];
let lastElement = array.pop();

// array -> [1,2,3,4];
// lastElement = 5;arr.slice(-1)[0] 
Source

Also in JavaScript: