xxxxxxxxxx
let myArr = [1, 2, 3, 4, 5];
let arr = myArr[myArr.length - 1];
console.log(arr);
xxxxxxxxxx
var colors = ["red","blue","green"];
var green = colors[colors.length - 1]; //get last item in the array
xxxxxxxxxx
let array = [1,2,3,4,5]
let sliced = array.slice(-1)[0]
//OR
let popped = array.slice(-1).pop()
//OR
let lengthed = array[array.length - 1]
xxxxxxxxxx
let nums = [1,2,3,4,5];
let lastOne = nums.pop();
// -> lastOne = 5
// -> nums = [1,2,3,4];
xxxxxxxxxx
const myArray = [1, 2, 3, 4, 5];
// Using array indexing
const lastElement = myArray[myArray.length - 1];
console.log(lastElement); // This will print 5, which is the last element of the array
xxxxxxxxxx
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]
xxxxxxxxxx
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]