For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/array/fill.md.

fill es-toolkit

Fills array elements with a specified value. Modifies the original array directly.

const filled = fill(arr, value, start, end);
If you don't want to modify the original array, use

toFilled.

toFilled returns a new array instead of modifying the original.

Usage

fill(arr, value, start?, end?)

Use fill when you want to fill a specific range of an array with a specified value. It replaces elements from the start position to just before the end position with the provided value. If you don't specify start or end positions, it fills the entire array.

import { fill } from 'es-toolkit/array';

// Fill the entire array with 'a'.
const array1 = [1, 2, 3];
fill(array1, 'a');
// Returns: ['a', 'a', 'a']

// Fill an empty array with 2.
const array2 = Array(3);
fill(array2, 2);
// Returns: [2, 2, 2]

// Fill from index 1 to just before 3 with '*'.
const array3 = [4, 6, 8, 10];
fill(array3, '*', 1, 3);
// Returns: [4, '*', '*', 10]

You can also use negative indices. Negative indices count from the end of the array.

import { fill } from 'es-toolkit/array';

const array = [1, 2, 3];
fill(array, '*', -2, -1);
// Returns: [1, '*', 3]

Parameters

arrArray<T | U>required

The array to fill.

valueUrequired

The value to fill the array with.

startnumberoptional

The start position. Default is 0.

endnumberoptional

The end position. Default is the array length.

Returns

Returns the original array filled with values.

Array<T | U>
Source: es-toolkit

Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.