Skip to content

head

Import from @varavel/vdl-plugin-sdk/utils/arrays.

Utility functions re-exported from es-toolkit (MIT License). See https://github.com/toss/es-toolkit for more details.

Call Signature

function head<T>(arr): T;

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.

Type Parameters

Type Parameter Description
T The type of elements in the array.

Parameters

Parameter Type Description
arr readonly [T, T] A non-empty array from which to get the first element.

Returns

T

The first element of the array.

Example

const arr = [1, 2, 3];
const firstElement = head(arr);
// firstElement will be 1

Call Signature

function head<T>(arr): T | undefined;

Returns the first element of an array or undefined if the array is empty.

This function takes an array and returns the first element of the array. If the array is empty, the function returns undefined.

Type Parameters

Type Parameter Description
T The type of elements in the array.

Parameters

Parameter Type Description
arr readonly 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.

Example

const emptyArr: number[] = [];
const noElement = head(emptyArr);
// noElement will be undefined