get last element of array javascript2 code example
Example 1: javascript get last element in array
var array =[1,2,3,4];
var last= array[array.length-1];
Example 2: 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];