JavaScript (ES2026) reference · Array Methods

JavaScript Array fill() Method

Fill all or part of an array with a static value, changing it in place. Often used to create an array of defaults.

JavaScript (ES2026) Since ES2015

Syntax

array.fill(value, start, end)

Parameters

ParameterDescription
value The value to fill with.
start, end optional Only fill this range.

Return value

The modified array.

Examples

Fill an arrayTry it yourself
console.log(new Array(3).fill(0));
console.log([1, 2, 3, 4].fill(9, 1, 3));

Output

[ 0, 0, 0 ]
[ 1, 9, 9, 4 ]
Objects are sharedTry it yourself
const bad = new Array(2).fill([]);
bad[0].push("x");
console.log(bad); // the same array twice!
const good = Array.from({ length: 2 }, () => []);
good[0].push("x");
console.log(good);

Output

[ [ 'x' ], [ 'x' ] ]
[ [ 'x' ], [] ]

Version history

VersionChange
JavaScript ES2015Added