572 B
572 B
title, type, language, tags, cover, dateModified
| title | type | language | tags | cover | dateModified | |
|---|---|---|---|---|---|---|
| Last array element | snippet | javascript |
|
white-laptop | 2020-10-20T23:02:01+03:00 |
Returns the last element in an array.
- Check if
arris truthy and has alengthproperty. - Use
Array.prototype.lengthto compute the index of the last element of the given array and return it, otherwise returnundefined.
const last = arr => (arr && arr.length ? arr[arr.length - 1] : undefined);
last([1, 2, 3]); // 3
last([]); // undefined
last(null); // undefined
last(undefined); // undefined