last

Get the last element of an array safely.

#array #utility
export const last = <T>(arr: readonly T[]): T | undefined =>
arr[arr.length - 1];
// Usage
last([1, 2, 3]); // 3
last([]); // undefined

Share this snippet

Comments