javascript last in a list 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: get last item in array js

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

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: javascript last in a list

let list = ["item1", "item2", "item3", "etc"];
let last = list.reverse()[0]
// or
let last = list[list.length - 1]
// or
let last = list.slice(-1)

Example 5: get last item of array javascript

get last item of array javascript