get last element array code example

Example 1: javascript get last element of array

var foods = ["kiwi","apple","banana"];
var banana = foods[foods.length - 1]; // Getting last element

Example 2: how to get last element of array

return array[array.length - 1];

Example 3: javascript last element of array

arr.slice(-1)[0]

Example 4: javascipt get last element of array

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

Example 5: get last element of array javascript

// Method - 1 ([] operator)
const arr = [5, 3, 2, 7, 8];
const last = arr[arr.length - 1];
console.log(last);
/*
    Output: 8
*/

// Method - 2 (Destructuring Assignment)
const arr = [5, 3, 2, 7, 8];

Example 6: js get last array element

const array = [1, 2, 3, 4]
const lastArrayElement = array[array.length - 1]