跳到主要内容

last

TS JS Deno

Returns the last element in an array.

Check if arr is truthy and has a length property, use arr.length - 1 to compute the index of the last element of the given array and return it, otherwise return undefined.

typescript
const last = (arr: any[] = []) => arr[arr.length - 1];
typescript
last([1, 2, 3]); // 3
last([]); // undefined
last([null]); // null
last(undefined); // undefined