Software engineer & writer.

Published elsewhere

Selected writing for engineering publications.

External
Silo architecture: split crates2026What Silo does with your master passwordBuilding Silo Tray: the background companionQuiet Mornings and Slow Brewed CoffeeA fresh StartFood for ThoughtHello [ Your_Name ]JavaScript Array.at() method2021Best 19 React UI Component Libraries of 20262023SvelteKit vs. Next.js: Which Should You Choose in 2026?2024Popper vs. Floating UI2022Axios POST requests: Handling errors, authentication, and best practices2025Understanding React’s useEffect cleanup function2024Understanding the CSS revert-layer keywordA guide to enabling partial pre-rendering in Next.jsBuilding a progressive web app in Remix with Remix PWA2023Using pointerEvents in React Native2022Build an analytics app with React and Cube.js2021Understanding React Native env variables2022How to build a Marketing Site with React and StrapiGoogling your errors as a Junior Developer2020What is Git and GitHub2019Building CloudPlexo's design system, part 1: Choosing type, icons, and a buttonNEW2026Building CloudPlexo's design system, part 2: Testing the foundations in interfacesNEWBuilding CloudPlexo's design system, part 3: From decisions to documentationNEW

JavaScript Array.at() method

#javascript
November 2, 2021

Before the arrival of Array.at() method, JavaScript developers have used many and different ways to return items from an array but there was still need to use negative indexing to get the last item. Array[0] will return the first item but to return the last item you would use Array[ Array.length - 1 ] as described below;

const names = [ 'Billy', 'Tony', 'Peter', 'Timothy', 'Andrew']

const getFirstName = names[0]
console.log(getFirstName)  // Billy

const getLastName = names[names.length - 1]
console.log(getLastName)  // Andrew

This has been one of the ways to achieve this. You can also do this with other methods like pop, slice and splice. See below;

// pop
const names = [ 'Billy', 'Tony', 'Peter', 'Timothy', 'Andrew']
const getLastName = names.pop()
console.log(getLastName)  // Andrew


// slice
const names = [ 'Billy', 'Tony', 'Peter', 'Timothy', 'Andrew']
const getLastName = names.slice(-1)
console.log(getLastName)  // ['Andrew']


// splice
const names = [ 'Billy', 'Tony', 'Peter', 'Timothy', 'Andrew']
const getLastName = names.splice(-1)
console.log(getLastName)  // ['Andrew']

All the above methods returns the last item but one thing to note; the splice method modifies the array unlike the others. That means it overwrites the original array and returns a new array after the splice method has been performed.

// splice
const names = [ 'Billy', 'Tony', 'Peter', 'Timothy', 'Andrew']
const getLastName = names.splice(-1)

console.log(getLastName)  // ['Andrew']
console.log(names)  // [ 'Billy', 'Tony', 'Peter', 'Timothy' ]

Using Array.at() method

Array.at() method makes it very easy to return the last item. With the Array.at(), you can now easily get the last item using negative indexing. This method takes an integer value and returns the item at that index, allowing for positive and negative integers.

const names = [ 'Billy', 'Tony', 'Peter', 'Timothy', 'Andrew']
const getLastName = names.at(-1)
console.log(getLastName) // Andrew

const names = [ 'Billy', 'Tony', 'Peter', 'Timothy', 'Andrew']
const getSecondLastName = names.at(-2)
console.log(getSecondLastName) // Timothy

const names = [ 'Billy', 'Tony', 'Peter', 'Timothy', 'Andrew']
const getFirstName = names.at(0)
console.log(getFirstName) // Billy

Array.at() method also works on strings too

const myName = 'Chimezie Innocent'
const getFirstLetter = myName.at(0)
console.log(getFirstLetter) // C

const myName = 'Chimezie Innocent'
const getLastLetter = myName.at(-1)
console.log(getLastLetter) // t