javascrip get last element of array code example

Example 1: how to get last element of array

return array[array.length - 1];

Example 2: find last element in array javascript

var heroes = ["Batman", "Superman", "Hulk"];
var lastHero = heroes.pop(); // Returns last elment of the Array
// lastHero = "Hulk"

Example 3: javascipt get last element of array

const arr = [1,2,3] ;
console.log(arr[arr.length - 1]);

Example 4: javascript get last element of array

if (loc_array[loc_array.length - 1] === 'index.html') {
   // do something
} else {
   // something else
}

Tags:

Java Example