docs/reference/compat/array/last.md
::: warning Use es-toolkit's last
This last function behaves complexly due to handling null or undefined.
Instead, use the faster and more modern es-toolkit's last.
:::
Returns the last element of an array.
const lastElement = last(array);
last(array)Use last when you want to get the last element of an array. Returns undefined if the array is empty.
import { last } from 'es-toolkit/compat';
// Last element of a number array
last([1, 2, 3, 4, 5]);
// Returns: 5
// Last element of a string array
last(['a', 'b', 'c']);
// Returns: 'c'
// Last element of an object array
const users = [{ name: 'Alice' }, { name: 'Bob' }];
last(users);
// Returns: { name: 'Bob' }
Empty arrays or null, undefined return undefined.
import { last } from 'es-toolkit/compat';
// Empty array
last([]);
// Returns: undefined
// null array
last(null);
// Returns: undefined
// undefined array
last(undefined);
// Returns: undefined
Array-like objects are also supported.
import { last } from 'es-toolkit/compat';
// Array-like object
const arrayLike = { 0: 'first', 1: 'second', length: 2 };
last(arrayLike);
// Returns: 'second'
// Strings are also array-like objects
last('hello');
// Returns: 'o'
array (ArrayLike<T> | null | undefined): The array to get the last element from.(T | undefined): Returns the last element of the array, or undefined if the array is empty, null, or undefined.