mirror of
https://github.com/toss/es-toolkit.git
synced 2024-12-04 09:44:59 +03:00
1.0 KiB
1.0 KiB
head
Returns the first element of an array.
This function takes an array and returns the first element of the array. If the array is empty, the function returns undefined
.
Signature
export function head<T>(arr: readonly [T, ...T[]]): T;
export function head<T>(arr: readonly T[]): T | undefined;
Parameters
arr
(T[]
): The array from which to get the first element.
Returns
(T | undefined
): The first element of the array, or undefined
if the array is empty.
Examples
const arr1 = [1, 2, 3];
const firstElement1 = head(arr1);
// firstElement1 will be 1
const arr2: string[] = [];
const firstElement2 = head(arr2);
// firstElement2 will be undefined
const arr3 = ['a', 'b', 'c'];
const firstElement3 = head(arr3);
// firstElement3 will be 'a'
const arr4 = [true, false, true];
const firstElement4 = head(arr4);
// firstElement4 will be true
const arr5: [number, string, boolean] = [1, 'a', true];
const firstElement5 = head(arr5);
// firstElement5 will be 1