Use Array.prototype.at() for cleaner relative index access in JavaScript
Replace arr[arr.length - 1] with arr.at(-1) for readable negative index access in modern JavaScript.
25 Aug 2026, 01:27 UTC

Stop writing arr[arr.length - 1]
If you need the last item in a JavaScript array, you have probably written something like this:
const last = items[items.length - 1];
It works, but it is verbose, and it is easy to mistype the arithmetic when you want the second-to-last or third-to-last item. Worse, JavaScript bracket notation does not accept negative indices. items[-1] does not reach backward from the end; it reads a property literally named "-1", which is almost never what you want.
What Array.prototype.at() does
Array.prototype.at() is an ECMAScript 2022 method that accepts an integer index and returns the element at that position. Negative values count backward from the end of the array. It does not mutate the array.
const items = ["first", "second", "third", "fourth"];
items.at(-1); // "fourth"
items.at(-2); // "third"
items.at(0); // "first"
items.at(10); // undefined
If the index is out of range, at() returns undefined. The method is generic: it works on any array-like object with a length property and numeric properties, including strings and DOM NodeList objects.
Worked example: carousel neighbors
Imagine a carousel component that needs the current image and its neighbors without mutating the source array.
function getCarouselItems(images, currentIndex) {
return {
previous: images.at(currentIndex - 1),
current: images.at(currentIndex),
next: images.at(currentIndex + 1),
};
}
const images = ["img-a.jpg", "img-b.jpg", "img-c.jpg"];
getCarouselItems(images, 1);
// { previous: "img-a.jpg", current: "img-b.jpg", next: "img-c.jpg" }
getCarouselItems(images, 0);
// { previous: undefined, current: "img-a.jpg", next: "img-b.jpg" }
Because at() returns undefined at the boundaries, the calling code can hide navigation buttons instead of guarding against negative array indices manually.
Limitations and fallbacks
- Older runtimes: Internet Explorer and Node.js earlier than 16.6 do not support
at(). Use a polyfill or fall back toarr[arr.length - 1]when required. - Integer indices only: Passing a float throws a
TypeError. Objects are coerced to integers, which can produce surprising results. - Undefined ambiguity:
at()returnsundefinedboth for out-of-range indices and for arrays that genuinely containundefined. Check the array length first if you must distinguish between "missing" and "explicitly undefined".
Verify support
Run this in your terminal. No special permissions are required because the command is read-only.
node -e "console.log([1, 2, 3].at(-1))"
Expected output: 3. If you see TypeError: [1, 2, 3].at is not a function, your Node version is below 16.6.
In browser DevTools, evaluate ["a", "b", "c"].at(-2). Expected output: "b".
Takeaway
Use at(-1) instead of arr[arr.length - 1] when you want the last element, and use at(-2), at(-3), and so on for relative positions. It is shorter, harder to misindex, and works on strings and array-like objects. Confirm your target runtime supports it, and remember that undefined means either out-of-range or an explicit undefined element.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.