Skip to content

unshift ​

The unshift() function adds one or more elements to the beginning of the array and returns an enriched copy.

Interactive example ​

Syntax ​

Classic signature ​

typescript
function unshift<
	GenericElement extends unknown
>(
	input: readonly GenericElement[],
	element: NoInfer<GenericElement>,
	...elements: NoInfer<GenericElement>[]
): GenericElement[]

Curried signature ​

typescript
function unshift<
	GenericElement extends unknown
>(
	element: NoInfer<GenericElement>
): (input: readonly GenericElement[]) => GenericElement[]

Parameters ​

  • input: Source array.
  • element, ...elements: Values inserted at the beginning (in the order provided).

Return value ​

A new array whose prefix matches the added values, followed by the former content.

See also ​

  • shift - Removes the first element
  • push - Adds at the end of the array

Sources ​

Released under the MIT license.