500 B
500 B
title, type, language, tags, cover, dateModified
| title | type | language | tags | cover | dateModified | |
|---|---|---|---|---|---|---|
| Head of array | snippet | javascript |
|
clay-pot-horizon | 2020-10-19T22:49:51+03:00 |
Returns the head of an array.
- Check if
arris truthy and has alengthproperty. - Use
arr[0]if possible to return the first element, otherwise returnundefined.
const head = arr => (arr && arr.length ? arr[0] : undefined);
head([1, 2, 3]); // 1
head([]); // undefined
head(null); // undefined
head(undefined); // undefined