JavaScript Array Last Element Get Get
const arr = [1, 2, 3, 4];
const lastElement = arr[arr.length - 1];
console.log(lastElement);
Abir Hasan
const arr = [1, 2, 3, 4];
const lastElement = arr[arr.length - 1];
console.log(lastElement);
// 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];
if (locArray.at(-1) === 'index.html') {
// do something
} else {
// something else
}
let arry = [2, 4, 6, 8, 10, 12, 14, 16];
let lastElement = arry[arry.length - 1];
console.log(lastElement);
//Output: 16
if (loc_array[loc_array.length - 1] === 'index.html') {
// do something
} else {
// something else
}
const lastItem = colors[colors.length - 1]