4Geeks chosen to deliver AI education in the Bahamas alongside Harvard, Oxford, and Columbia.See more

How to get the last element of an Array in Javascript?

By 4Geeks

Read the last element with at(-1) or length - 1, remove it with pop(), take the last few with slice() and avoid the arr[-1] trap.

5 min readLast updated
Last element of an array in JavaScript icon

How do you get the last element of an array in JavaScript?

Short answer: use at(-1). It counts from the end of the array and works in every modern browser and in Node.js 16.6 or newer. The classic way, which works everywhere, is arr[arr.length - 1].

javascript
const fruits = ["apple", "banana", "cherry"];
 
console.log(fruits.at(-1));               // cherry
console.log(fruits[fruits.length - 1]);   // cherry

Neither of them changes the array. If the array is empty, both return undefined.

at(-1) and length - 1 read the last element, fruits[-1] does not work in JavaScript and pop() removes the element as it returns it.

Every way to get the last element

MethodExampleChanges the array?Empty array
at(-1)fruits.at(-1)Noundefined
length - 1fruits[fruits.length - 1]Noundefined
slice(-1)fruits.slice(-1)[0]Noundefined
pop()fruits.pop()Yes, removes itundefined
findLast()nums.findLast(n => n > 10)Noundefined

For everyday code, use at(-1). Use pop() only when you also want to remove the element.

Get the last element with at(-1)

at() accepts negative indexes: -1 is the last element, -2 the one before, and so on.

javascript
const scores = [72, 85, 91, 64];
 
console.log(scores.at(-1));  // 64
console.log(scores.at(-2));  // 91
console.log(scores.at(0));   // 72

It also works with strings: "hello".at(-1) returns "o". at() was added in ES2022, so very old browsers such as Internet Explorer don't support it. If you must support them, use length - 1.

Get the last element with length - 1

Arrays start at index 0, so the last index is always length - 1:

javascript
const fruits = ["apple", "banana", "cherry"];
const lastIndex = fruits.length - 1;  // 2
 
console.log(fruits[lastIndex]);  // cherry

Writing fruits[fruits.length] returns undefined, because index 3 doesn't exist. And fruits[-1] also returns undefined: unlike Python, square brackets in JavaScript don't count from the end.

Career · AI Engineering for Developers

You know your way around arrays, now build AI apps with it

Use the coding skills you already have to build AI agents, RAG systems and LLM-powered apps, with mentors and real projects.

Get the last element and remove it with pop()

pop() returns the last element and removes it from the original array:

javascript
const tasks = ["write", "test", "deploy"];
const next = tasks.pop();
 
console.log(next);   // deploy
console.log(tasks);  // ["write", "test"]

This is useful for stacks, for example an undo history. If you only want to read the last element, don't use pop(): the array will lose it.

Get the last few elements with slice()

slice() with a negative number returns a new array with the last elements, without changing the original:

javascript
const nums = [10, 20, 30, 40, 50];
 
console.log(nums.slice(-1));     // [50]
console.log(nums.slice(-3));     // [30, 40, 50]
console.log(nums.slice(-1)[0]);  // 50

You can also use destructuring: const [last] = nums.slice(-1); stores 50 in last.

Get the last element that matches a condition

findLast() (ES2023) goes through the array from the end and returns the first element that passes your test. findLastIndex() returns its position:

javascript
const nums = [3, 8, 12, 5, 20, 7];
 
console.log(nums.findLast(n => n > 10));       // 20
console.log(nums.findLastIndex(n => n > 10));  // 4

Last element of an array of objects

The same methods work with objects. Use optional chaining (?.) in case the array is empty:

javascript
const users = [{ name: "Ana" }, { name: "Luis" }];
 
console.log(users.at(-1).name);   // Luis
console.log([].at(-1)?.name);     // undefined, no error

Without ?., an empty array throws TypeError: Cannot read properties of undefined (reading 'name').

Use AI to work with arrays

An assistant like ChatGPT, Claude or GitHub Copilot can help you pick the right array method. These three prompts are ready to copy.

1. Choose a method

I have this JavaScript array: [paste the array]. I need to get [the last element / the last 3 /
the last one that meets a condition] without changing the array. Show me the best method.

2. Explain an undefined

This JavaScript code returns undefined when I try to get the last element: [paste the code].
Explain why and fix it.

3. Support old browsers

My JavaScript code uses arr.at(-1) and must also work in [the browser]. Tell me if at() is
supported there and give me an alternative that works everywhere.

Test each answer in your browser console or with Node.js.

Learn with a mentor

From array methods to shipping AI features

In the 4Geeks AI Engineering for Developers program you add AI agents, RAG and production LLM apps to your portfolio.

Common problems when getting the last element

What you seeWhy it happensFix
arr[-1] returns undefinedBrackets don't count from the end in JavaScriptUse arr.at(-1)
arr[arr.length] returns undefinedThe last index is length - 1Use arr[arr.length - 1]
The array lost its last elementYou used pop(), which removes itUse at(-1) to only read it
slice(-1) returns [50], not 50slice() always returns an arrayAdd [0]: slice(-1)[0]
TypeError: Cannot read properties of undefinedThe array is emptyCheck arr.length or use ?.
TypeError: arr.at is not a functionThe value isn't an array, or the browser is very oldCheck with Array.isArray(), or use length - 1
Free download

The last element of an array in a 2-page cheat sheet

at(-1), length - 1, slice(), pop() and findLast(), empty arrays and objects, common errors and prompts for ChatGPT, Claude or Copilot. Free, in PDF.

Last element of an array in JavaScript cheat sheet

2 pages · PDF · 4Geeks

  • Every method side by side, with what it changes
  • The last few elements and the last match
  • Common errors and prompts to solve them with AI

Get it free

Your PDF right away

Frequently asked questions about the last element of an array

Want to go from JavaScript arrays to building AI apps, with real projects and mentors?

See the AI Engineering for Developers program