Use
The short answer
#Call at(-1) on the array. Negative numbers count back from the end, so -1 is the last item, -2 the second to last, and so on. It returns undefined for an empty array and does not change the original.
_10const steps = ['plan', 'build', 'ship'];_10_10steps.at(-1); // 'ship'_10steps.at(-2); // 'build'
Why it happens
#Square-bracket indexing in JavaScript does not support negative numbers. steps[-1] does not mean "from the end". It looks up a property literally called '-1', which does not exist, so you get undefined.
The traditional workaround is steps[steps.length - 1], which works but repeats the array name and is awkward for long expressions. The at() method was added to arrays, strings and typed arrays in ES2022 to fix this, and has been supported in all major browsers and Node.js for several years.
How to fix it
#Use at(-1)
This is the clearest option and works well at the end of a chain.
_10const latest = orders_10 .filter((order) => order.status === 'paid')_10 .at(-1);
It works the same way on strings.
_10'hello'.at(-1); // 'o'
Use length - 1 when you also need to assign
at() is read-only. To replace the last item, index it with length - 1.
_10const scores = [10, 20, 30];_10scores[scores.length - 1] = 35;_10console.log(scores); // [10, 20, 35]
In React state or anywhere you avoid mutation, use with() instead. It returns a copy with one item replaced and also accepts negative indexes.
_10const updated = scores.with(-1, 35); // scores is unchanged
Use slice(-n) for the last few items
slice with a negative start returns a new array of the last n items.
_10const messages = ['a', 'b', 'c', 'd', 'e'];_10messages.slice(-3); // ['c', 'd', 'e']_10messages.slice(-1); // ['e'] (an array, not the item)
Use pop() only when you want to remove it
pop() returns the last item and removes it from the array. That is right for a stack, but surprising if you only meant to read it.
_10const stack = [1, 2, 3];_10const top = stack.pop(); // 3_10console.log(stack); // [1, 2]
Use findLast() for the last match
If you want the last item that matches a condition, findLast searches from the end without copying or reversing the array. findLastIndex returns its position.
_10const lastError = logs.findLast((entry) => entry.level === 'error');
Common pitfalls
#- Using array[-1]. It returns
undefinedrather than throwing, so the bug can go unnoticed. Useat(-1). - Forgetting empty arrays.
[].at(-1)isundefined. Add a fallback with??if you need a default:items.at(-1) ?? null. - Using reverse() to get the end.
reverse()changes the original array in place. If you need a reversed copy, usetoReversed(). - Expecting slice(-1) to return the item. It returns a one-item array. Add
[0], or just useat(-1). - Array-like objects. A
NodeListfromquerySelectorAll, anHTMLCollectionand theargumentsobject are not arrays and have noatmethod. Convert withArray.from(list).at(-1). - TypeScript types.
at(-1)is typed asT | undefined, which is accurate. Handle theundefinedcase rather than asserting it away.
Related reading
#Was this resource helpful?