Get the last element from an array

Given an array with one or more elements, return only the last element from the array.

const array = [1, 2, 3];
array.at(-1); // 3 (preferred, does NOT mutate)
array.pop(); // 3 (preferred, mutates array!)
array.slice(-1)[0]; // 3 (for older browsers and NodeJS versions, mutates array!)

Related MDN Documentation

Broader Topics Related to JavaScript Recipe: Get the last element from an array

JavaScript and TypeScript recipes

JavaScript and TypeScript recipes

Quick and easy to copy recipes for JavaScript and TypeScript

JavaScript Recipe: Get the last element from an array Knowledge Graph