Typescript Get Last Element of Array [Using 3 methods]

When working with arrays in TypeScript, a common task is to retrieve the last element. In this Typescript tutorial, we’ll explore several methods to get the last element of an array in TypeScript, each with its own use case and advantages. Let us explore, “Typescript get last element of array”.

To get the last element of an array in TypeScript, you can use the array.length – 1 index, the array.at(-1) method, or array destructuring with slice(-1). For example, with an array cities = [‘New York’, ‘Los Angeles’, ‘Chicago’], cities[cities.length – 1], cities.at(-1), or […cities].pop() will all return ‘Chicago’, the last element.

Typescript get last element of array

In TypeScript, arrays are ordered collections of elements. Accessing the last element often involves identifying the length of the array and using this information to find the final item.

Here are the best 3 methods to get the last element of a Typescript array.

Method 1: Using Array Length

The most straightforward approach in TypeScript is to use the length property of the array. Since array indices start at 0, the last element’s index is length - 1.

Here is the complete code to get last element of array in Typescript.

let cities = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix'];

function getLastElement<Type>(array: Type[]): Type | undefined {
  if (array.length === 0) return undefined;
  return array[array.length - 1];
}

let lastCity = getLastElement(cities);
console.log(lastCity);

This function, getLastElement, is generic and works with any array type. It returns undefined for an empty array to safely handle edge cases.

Output:

Phoenix

Once you run the code using VS code, you can see the output in the screenshot below:

typescript get last element of array

Method 2: Using Array.prototype.at()

TypeScript 4.2 introduced the at() method, which allows you to get the element at a given index. One of the advantages of the at() method is that it accepts negative integers, which count back from the last element.

Here’s an example of using at() to get the last element in Typescript:

let cities = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix'];

function getLastCity(cities: string[]): string | undefined {
  return cities.at(-1);
}

let lastCity = getLastCity(cities);
console.log(lastCity);

Once you run the code, you can see the output in the screenshot below.

typescript last element of array

The at() method provides a more readable and modern way to access the last element, especially when dealing with TypeScript’s type safety features.

Method 3: Using Array slice() Method

Another approach is to use the Typescript slice() method, which returns a shallow copy of a portion of an array into a new array object. If you want to get the last element from the Typescript array, you can pass -1 as the start parameter.

Here is a complete code to get typescript array last element.

let cities = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix'];

function getLastCity(cities: string[]): string | undefined {
  const [last] = cities.slice(-1);
  return last;
}

let lastCity = getLastCity(cities);
console.log(lastCity);

Here is the output you can see in the screenshot below:

get last element of array typescript

Conclusion

Typescript offers multiple ways to retrieve the last element of an array, each suited for different scenarios. Using the array’s length property is straightforward and efficient for most cases, but it requires a check to ensure the array isn’t empty. The at() method, introduced in TypeScript 4.2, provides a modern and readable alternative, especially when dealing with negative indices. Lastly, the slice() method is versatile for extracting portions of an array and can be used to get the last element as well.

I hope you got an idea of “typescript get last element of array“, I have explained three different methods to get last element of array in Typescript.

You may also like:

>