xxxxxxxxxx
var array = ['red','green','yellow']
console.log(array[array.length-1])
xxxxxxxxxx
var colors = ["red","blue","green"];
var green = colors[colors.length - 1]; //get last item in the array
xxxxxxxxxx
var colors = ["red","blue","green"];
var green = colors[colors.length - 1]; //get last item in the array
xxxxxxxxxx
var numbers = ["one", "two", "three"];
var lastnumber = numbers[numbers.length - 1];
console.log(lastnumber);
xxxxxxxxxx
let nums = [1,2,3,4,5];
let lastOne = nums.pop();
// -> lastOne = 5
// -> nums = [1,2,3,4];
xxxxxxxxxx
const arr = [5, 3, 2, 7, 8];
const last = arr.at(-1);
console.log(last);
/*
Output: 8
*/
//Better and shorter way will be
const arr = [1,2,3,4]
console.log(arr.pop())
/*
Output: 4
*/
xxxxxxxxxx
const colors = ['black', 'white', 'red', 'yellow'];
const yellow = colors.at(-1);