arr
is truthy and has a length
property.arr[0]
if possible to return the first element, otherwise return undefined
.const head = arr => (arr && arr.length ? arr[0] : undefined);
head([1, 2, 3]); // 1
head([]); // undefined
head(null); // undefined
head(undefined); // undefined
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️