array last value code example

Example 1: last element of array javascript

var my_array = /* some array here */;
var last_element = my_array[my_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: get last item in array

let array = [1,2,3,4,5]

let sliced = array.slice(-1)[0]

//OR

let popped = array.slice(-1).pop()

//OR

let lengthed = array[array.length - 1]

Example 4: get last element in array in js

var array = ['red','green','yellow']
console.log(array[array.length-1])

Example 5: find last item in an array JS

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

Tags:

Java Example