How to Fetch the Last Object in an Array in JavaScript?

Estimated read time 1 min read

You can fetch the last object in an array in JavaScript using the following code:

const array = [1, 2, 3, 4, 5];
const lastObject = array[array.length - 1];

console.log(lastObject);
// Output: 5

In this example, array is an array of numbers and array.length - 1 returns the index of the last object in the array. The expression array[array.length - 1] then returns the last object itself.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply