Skip to main content

How to Get the Second to Last Element of an Array in JavaScript

Accessing elements from the end of an array is a common task, and getting the second to last element is a frequent requirement. For example, you might need to find the previous state in a history array or access a value just before the end of a dataset. Modern JavaScript provides a clean and highly readable method for this.

This guide will teach you the best methods for getting the second to last element of an array. We will cover the modern and recommended at() method and contrast it with the classic approach of calculating the index with .length.

The Core Concept: Negative Indexing

Many modern programming languages allow you to access array elements by counting backward from the end using negative numbers. The Array.prototype.at() method brings this powerful and intuitive feature to JavaScript.

  • array.at(-1) gets the last element.
  • array.at(-2) gets the second to last element.
  • And so on.

The at() method is the standard, built-in tool for this job. It is the most readable and concise way to access elements from the end of an array.

For example, you have an array and you want to get the second to last element.

// Problem: How to get the element 'c'?
const letters = ['a', 'b', 'c', 'd'];

Solution: this single, readable line is all you need.

const letters = ['a', 'b', 'c', 'd'];

const secondToLast = letters.at(-2);

console.log(secondToLast);
// Output: "c"
note

This is the recommended best practice because its intent is perfectly clear.

The Classic Method: Bracket Notation [] with .length

Before the at() method was introduced, the only way to get an element from the end of an array was to manually calculate its index using the array's length.

The logic:

  • The last element is at index array.length - 1.
  • Therefore, the second to last element is at index array.length - 2.

Solution:

const letters = ['a', 'b', 'c', 'd'];

const secondToLast = letters[letters.length - 2];

console.log(secondToLast);
// Output: "c"
note

While this works perfectly, at(-2) is generally preferred for its superior readability and for avoiding manual index calculation, which can be a source of "off-by-one" errors.

How to Handle Short Arrays (Fewer Than Two Elements)

A robust solution must handle cases where the array is too short to have a second to last element. Both recommended methods handle this gracefully by returning undefined.

Solution:

const shortArray = ['a'];
const emptyArray = [];

// Using .at()
console.log(shortArray.at(-2)); // Output: undefined
console.log(emptyArray.at(-2)); // Output: undefined

// Using bracket notation
console.log(shortArray[shortArray.length - 2]); // Output: undefined
console.log(emptyArray[emptyArray.length - 2]); // Output: undefined
note

In both cases, attempting to access the second to last element of a short array safely returns undefined, which you can easily check for in your code.

Conclusion

For getting the second to last element of an array, modern JavaScript provides a clear and superior solution.

  • The array.at(-2) method is the recommended best practice. It is the most concise, readable, and idiomatic way to access elements by counting from the end of the array.
  • The classic array[array.length - 2] syntax is a perfectly functional alternative but is more verbose and slightly less intuitive than at().

For writing clean, modern, and maintainable code, you should prefer the at() method for any end-of-array access.