Skip to content

fill

The fill() function fills a portion of an array with a given value and returns a modified copy.

Interactive example

Syntax

Classic signature

typescript
function fill<
	GenericElement extends unknown
>(
	input: readonly GenericElement[],
	element: GenericElement, 
	start: number, 
	end: number
): GenericElement[];

Curried signature

typescript
function fill<
	GenericElement extends unknown
>(
	element: GenericElement, 
	start: number, 
	end: number
): (input: readonly GenericElement[]) => GenericElement[];

Parameters

  • input: Source array.
  • element: Value that replaces each element of the targeted section.
  • start: Start index (inclusive).
  • end: End index (exclusive).

Return value

A new array whose section [start, end) contains element. The rest of the array remains unchanged.

See also

  • fillAll - Fills the entire array
  • set - Replaces a single index

Sources

Released under the MIT license.