Get the last Element of an Array in JavaScript

avatar

Borislav Hadzhiev

Tue Oct 05 20213 min read

Get the last Element of an Array in JavaScript #

Use the Array.at() method to get the last element of an array in JavaScript, e.g. const last = arr.at(-1). When passed a negative index, the at() method returns an element by counting back from the end of the array.

index.js
const arr = ['a', 'b', 'c']; const last = arr.at(-1); console.log(last); // 👉️ "c"

We used the Array.at method to get the last element of an array in JavaScript.

The only parameter the method takes is the index of the array element to be returned.

When passed a negative index, the method returns an element by counting back from the end of the array.

The at() method returns the element in the array that matches the provided index or undefined if the given index cannot be found.

You can check that the last variable does not store an undefined value in one of the following ways:

index.js
const arr = ['a', 'b', 'c']; const last = arr.at(-1); console.log(last); // 👉️ "c" if (last !== undefined) { console.log(last.toUpperCase()); // 👉️ "C" } console.log(last?.toUpperCase()); // "C"

The if statement and the optional chaining (?.) operator exclude the possibility of the value being undefined, which enables us to use type-specific built-in methods.

The optional chaining (?.) operator short-circuits, instead of throwing an error, if the reference is undefined or null.

Alternatively, you can access the element at the last array index.

To get the last element of an array in JavaScript, access the array at index array.length - 1, e.g. const last = arr[arr.length - 1]. The calculation evaluates to the index of the last element in the array.

index.js
const arr = ['a', 'b', 'c']; const lastElement = arr[arr.length - 1]; console.log(lastElement); // 👉️ c
Indexes are zero-based in JavaScript, that's why we have to subtract 1 from the array's length to get the index of the last element in the array.

Other ways to get the last element of an array, include Array.pop and Array.slice.

You can get the last element of an array, by calling the pop method on the array. The pop method mutates the original array, removes and returns its last element.

index.js
const arr = ['a', 'b', 'c']; // 👇️ mutates the original array const lastElement = arr.pop(); console.log(lastElement); // 👉️ c console.log(arr) // 👉️ ['a', 'b']

The Array.pop method returns the last element of the array, but it also mutates the original array.

Calling the pop method on the array removes the last element, leaving the array with just 2 elements. This can be confusing if you have to perform other operations on the array.

In general, operations that cause mutation are considered a bad practice, because they're difficult to track and reason about.

To get the last element of an array:

  1. Pass -1 as an argument to the Array.slice method
  2. The method returns a new array containing the last element from the original array
  3. Assign the element from the new array to a variable
index.js
const arr = ['a', 'b', 'c']; const lastElement = arr.slice(-1)[0]; console.log(lastElement); // 👉️ c

Passing -1 to the Array.slice method means that we want a new array containing only the last element of the original array.

We then assign the only element in the new array to a variable.

The Array.slice method does not mutate the original array, instead it returns a new array.

Further Reading #

Use the search field on my Home Page to filter through my more than 1,000 articles.