How to get last element of an array in JavaScript?
By Flavio
Published · Updated
Tagged: JavaScript
Learn how to get the last element of a JavaScript array with at(-1), the array.length - 1 alternative, slice(-1), the last N items, the last item that matches a condition, and the pop() pitfall.
Are you wondering how to get last element of an array in JavaScript?
Suppose you have an array, like this:
const colors = ['red', 'yellow', 'green', 'blue']
In this case the array has 4 items.
You know you can get the first item using colors[0], the second using colors[1] and so on.
Use at(-1)
The clearest modern solution is at(-1):
const lastItem = colors.at(-1) // 'blue'
A negative index counts from the end of the array. -1 means the last item, -2 means the second-to-last item, and so on:
colors.at(-2) // 'green'
at() works in every current browser and in Node.js 16.6 and later. It’s been available across browsers since March 2022.
Use array.length - 1
If you need to support an older JavaScript environment, use the array length:
const lastItem = colors[colors.length - 1] // 'blue'
Arrays start at index 0, so the last index is the length minus 1. colors.length is 4, and the last item is at index 3.
Why colors[-1] doesn’t work
If you come from Python, you might try this:
colors[-1] // undefined
JavaScript doesn’t support negative indexes with square brackets. It treats -1 as a property name, the string '-1', and the array has no property with that name. So you get undefined, with no error.
That’s exactly the problem at() solves.
Empty arrays
Both versions return undefined when the array is empty:
const empty = []
empty.at(-1) // undefined
empty[empty.length - 1] // undefined
If undefined can also be a real value in your array, check the length instead:
if (colors.length > 0) {
console.log(colors.at(-1))
}
Don’t use pop() just to read the last item
pop() returns the last item too, but it also removes it from the array:
const items = ['red', 'yellow', 'green', 'blue']
items.pop() // 'blue'
console.log(items) // [ 'red', 'yellow', 'green' ]
Use it only when you really want to remove the item.
Get the last N items with slice()
slice() also accepts negative numbers. It returns a new array and doesn’t change the original:
colors.slice(-2) // [ 'green', 'blue' ]
colors.slice(-1) // [ 'blue' ]
Note that slice(-1) gives you an array with the last item, not the item itself. Use it when you want the last few items. For a single item, at(-1) is simpler.
If you ask for more items than the array has, you get the whole array.
Get the last item that matches a condition
To find the last item that passes a test, use findLast(). It searches from the end of the array:
const numbers = [1, 5, 8, 3, 10, 4, 7]
numbers.findLast((n) => n % 2 === 0) // 4
It returns undefined when nothing matches. If you need the position instead of the value, use findLastIndex(), which returns -1 when nothing matches.
findLast() needs Node.js 18 or later, and it’s been available across browsers since August 2022.
It works on strings too
Strings have an at() method with the same behavior, so you can get the last character of a string:
'hello'.at(-1) // 'o'
To get the first items instead, see how to get the first items in an array.
Want me to talk about your product? You can sponsor this site.
Related posts about js: