how to get the last element of an array in javascript

JavaScript
arr.slice(-1)[0] const nums = [1, 2, 3, 4, 5, 6, 7];
const lastOne = nums[nums.lenght - 1]; // last element of arrayBest IDE for Web Developers in Visual Studio Codeif (loc_array[loc_array.length - 1] === 'index.html') {
   // do something
} else {
   // something else
}const heroes = ["Batman", "Superman", "Hulk"];
const lastHero = heroes.pop(); // Returns last elment of the Array
// lastHero = "Hulk"var colors = ["red","blue","green"];
var green = colors[colors.length - 1];//get last item in the array
Source

Also in JavaScript: