Skip to content

slice

The slice() function creates a copy of a portion of an array between start and end (excluded) without modifying the original. It mirrors Array.prototype.slice with a curried API.

Interactive example

Syntax

Classic signature

typescript
function slice<
  GenericElement extends unknown,
>(
  array: readonly GenericElement[],
  start?: number,
  end?: number,
): GenericElement[]

Curried signature

typescript
function slice<
  GenericElement extends unknown,
>(
  start?: number,
  end?: number,
): (array: readonly GenericElement[]) => GenericElement[]

Parameters

  • array: Source array.
  • start: Starting index (inclusive). Defaults to 0.
  • end: Ending index (exclusive). Defaults to the array length.

Return value

A new portion of the original array between start and end.

See also

  • at - Accesses a specific element
  • chunk - Splits an array into chunks

Released under the MIT license.