Files
30-seconds-of-code/snippets/head.md
Angelos Chalaris 980499db57 Update head.md
2019-12-11 09:52:00 +02:00

393 B

title, tags
title tags
head array,beginner

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