JavaScript Array.prototype.fill() Method
The fill() method sets all or part of an array to one value, changing the array in place. It's often used to create an array with a starting value.
Description
The fill() method changes elements of the array, from start up to (but not including) end, to a single value. It modifies the array in place and returns it.
A common pattern is new Array(5).fill(0) to make an array of five zeros. Be careful when filling with an object: every element will be the same object, so changing one changes them all. Use Array.from({ length: n }, () => ({})) to create separate objects.
fill() also works on typed arrays, where it's a fast way to reset memory. To create a two-dimensional grid, avoid new Array(3).fill([]), which shares one inner array between all rows; use Array.from({ length: 3 }, () => []) so each row is a separate array.
Syntax
array.fill(value)
array.fill(value, start, end)Parameters
- valueThe value to fill the array with.
- startOptional. The index to start at. Defaults to 0; negative values count from the end.
- endOptional. The index to stop before. Defaults to the array's length.
Return Value
The modified array.
Example
Browser Support
The following information will show you the current browser support for the Array.prototype.fill() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





