arr
is truthy and has a length
property.Array.prototype.length - 1
to compute the index of the last element of the given array and return it, otherwise return undefined
.const last = arr => (arr && arr.length ? arr[arr.length - 1] : undefined);
last([1, 2, 3]); // 3
last([]); // undefined
last(null); // undefined
last(undefined); // undefined
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️