Skip to content

last ​

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

Interactive example ​

Syntax ​

typescript
function last<
	GenericInput extends readonly unknown[]
>(
	input: GenericInput
): GenericInput extends readonly [...any[], infer InferredValue] 
	? InferredValue 
	: GenericInput[number] | undefined

Parameters ​

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

Return value ​

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

See also ​

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

Sources ​

Released under the MIT license.