find last index of array code example

Example 1: Javascript get last item in array

var colors = ["red","blue","green"];
var green = colors[colors.length - 1]; //get last item in the array

Example 2: find last element in array nodejs

var a = loc_array.slice(-1)[0]

Example 3: get the last item in an array

let array = [0, 1, 2, 3, 4, 5, 6, 7] 
console.log(array.slice(-1));
>>>[7]
console.log(array.slice(-2));
>>>[6, 7]
console.log(array.slice(-3));
>>>[5, 6, 7]

Example 4: how to get last element of an array in swifg

let myArray = ["Hello!", "World!"]
let capacity = myArray.count
let lastElement = myArray[capacity-1]
print(lastElement)

Example 5: last index of array js

array.lastIndexOf(searchElement[, fromIndex]);

Example 6: Get the last item in an array

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