docs/reference/compat/array/head.md
::: warning Use head from es-toolkit
This head function operates slowly due to ArrayLike object processing and array conversion process.
Instead, use the faster and more modern head from es-toolkit.
:::
Returns the first element of an array.
const firstElement = head(array);
head(array)Returns the first element of an array or array-like object. Returns undefined if the array is empty or invalid.
import { head } from 'es-toolkit/compat';
// First element of number array
const numbers = [1, 2, 3, 4];
const first = head(numbers);
// first is 1
// First element of string array
const strings = ['a', 'b', 'c'];
const firstChar = head(strings);
// firstChar is 'a'
// Array-like object
const arrayLike = { 0: 'x', 1: 'y', 2: 'z', length: 3 };
const firstItem = head(arrayLike);
// firstItem is 'x'
Empty arrays or invalid inputs return undefined.
import { head } from 'es-toolkit/compat';
const emptyArray: number[] = [];
const noElement = head(emptyArray);
// noElement is undefined
head(null); // undefined
head(undefined); // undefined
array (ArrayLike<T> | null | undefined): The array or array-like object to get the first element from.(T | undefined): Returns the first element of the array, or undefined if the array is empty or invalid.