Array.prototype.fill()

Fills all or part of an array with a static value in place.

Since ES2015 Spec ↗

Syntax

arr.fill(value, start, end)

Parameters

NameTypeRequiredDescription
value any Yes The value to fill the array with.
start number No Index to start filling. Negative counts from the end. Defaults to 0.
end number No Index before which to stop (exclusive). Negative counts from the end. Defaults to array length.

Returns

Array — The modified array (same reference).

Examples

console.log([1, 2, 3, 4].fill(0, 1, 3));
Output
[ 1, 0, 0, 4 ]
console.log(new Array(3).fill('x'));
Output
[ 'x', 'x', 'x' ]

Notes

Mutates the array in place. When filling with an object, every slot references the same object. Commonly used with `Array(n).fill()` to initialize arrays.

Browser & runtime support

EnvironmentSince version
chrome 45
firefox 31
safari 7.1
edge 12
node 4.0

See also