How to get last element of an array in JavaScript

Jan 14, 2024#javascript#arrays

In JavaScript, an array is a special type of object used to store and organize multiple values. Unlike objects, which use named keys, arrays use numeric indices to access elements. The elements in an array can be of any data type, and they can be accessed and manipulated using various array methods.

Arrays are zero-based indexed, which means that the indices of elements in an array start at 0. The first element is at index 0, the second element is at index 1, and so on, and the last element is array.length - 1. JavaScript returns undefined instead of throwing errors when you access an index that is out of bounds.

const arr = [1, 2, 3, "four", true];

console.log(arr[0]); // 1
console.log(arr.length); // 5
console.log(arr[arr.length - 1]); // true
console.log(arr[arr.length]); // undefined

There are different ways to get the last element of an array in JavaScript.

  1. Using square bracket notation [] with length property, which returns the number of elements in that array. It is a built-in property that can be accessed and modified. The length property is always one more than the highest array index.
const arr = [1, 2, 3, 4, 5];
const last = arr[arr.length - 1];
console.log(last); // 5
console.log(arr); // [1, 2, 3, 4, 5]

You can also use the length property to modify the size of an array. If you set the length property to a value less than the current number of elements, the array will be truncated. If you set it to a value greater than the current number of elements, the array will be extended, and new elements will be initialized with undefined.

const arr = [1, 2, 3, 4, 5];
console.log(arr.length); // 5

arr.length = 3;
console.log(arr); // [1, 2, 3]

arr.length = 7;
console.log(arr); // [1, 2, 3, <4 empty items>]
  1. Using at() method, relatively new addition to JavaScript since ES2022, which allows you to access elements in an array using a numeric index.

It’s similar to square bracket notation [], but with the added flexibility of supporting negative indices: array.at(-1) returns the last element, array.at(-2) returns the second to last, and so on.

const arr = [1, 2, 3, 4, 5];
const last = arr.at(-1);
console.log(last); // 5
console.log(arr); // [1, 2, 3, 4, 5]
  1. Using slice() method, which returns a new array containing a portion of the original array, without modifying the original array.

This method takes two optional parameters: start and end, which specify the index of the first and the last element to be included in the new array. If start or end are negative, they are counted from the end of the array. If start or end are omitted, they default to 0 and the length of the array, respectively.

const arr = [1, 2, 3, 4, 5];
const last = arr.slice(-1)[0];
console.log(last); // 5
console.log(arr); // [1, 2, 3, 4, 5]
  1. Using pop() method, which removes and returns the last element of an array. It also changes the length of the array by one. It returns undefined if called on empty array.
const arr = [1, 2, 3, 4, 5];
const last = arr.pop();
console.log(last); // 5
console.log(arr); // [1, 2, 3, 4]

Make sure to use pop() when you specifically need to remove the last element, and be aware that it modifies the original array. If you want to retrieve the last element without modifying the array, you might consider using previous methods.