Files
30-seconds-of-code/snippets/head.md
Isabelle Viktoria Maciohsek 27c168ce55 Bake date into snippets
2021-06-13 13:55:00 +03:00

475 B

title, tags, firstSeen, lastUpdated
title tags firstSeen lastUpdated
head array,beginner 2017-12-17T16:41:31+02:00 2020-10-19T22:49:51+03:00

Returns the head of an array.

  • 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