Skip to content

first ​

The first() method returns the first element of an array.

Interactive example ​

Syntax ​

typescript
function first<
	GenericInput extends readonly unknown[]
>(
	input: GenericInput
): GenericInput extends AnyTuple 
	? GenericInput[0] 
	: GenericInput[number] | undefined

Parameters ​

  • input: The array from which to retrieve the first element.

Return value ​

The first element of the array, or undefined if the array is empty.

See also ​

  • at - Returns the element at a given index
  • last - Returns the last element of the array

Sources ​

Released under the MIT license.