Returns the head of a list.
Check if arr
is truthy and has a length
property, use 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