Favorites

JavaScript Array.prototype.fill() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Array.prototype.fill()
JavaScript Array Instance methodThis method is supported by all modern browsers.
Updated 28th November 2026

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

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

const shared = new Array(2).fill({});
shared[0].a = 1;
console.log(shared);   // both elements changed

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.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee