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 arr = [1,2,3]
var lastelementofarr = [arr.length-1] //I think it will return the last element in the array
Source

Also in JavaScript: