how to get the last element in array javascript code example
Example 1: javascript get last element of array
var foods = ["kiwi","apple","banana"];
var banana = foods[foods.length - 1];
Example 2: how to get last item in array javascript
let nums = [1,2,3,4,5];
let lastOne = nums.pop();
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: get last element of array javascript
const arr = [5, 3, 2, 7, 8];
const last = arr[arr.length - 1];
console.log(last);
const arr = [5, 3, 2, 7, 8];
Example 5: javascript get last element in array