The fill() method of Array instances changes all elements within a range of indices in an array to a static value. It returns the modified array.
The fill() method of Array instances changes all elements within a range of indices in an array to a static value. It returns the modified array.
fill(value) fill(value, start) fill(value, start, end)
valueValue to fill the array with. Note all elements in the array will be this exact value: if value is an object, each slot in the array will reference that object.
start Optional
Zero-based index at which to start filling, converted to an integer.
start < 0, start + array.length is used.start < -array.length or start is omitted, 0 is used.start >= array.length, no index is filled.end Optional
Zero-based index at which to end filling, converted to an integer. fill() fills up to but not including end.
end < 0, end + array.length is used.end < -array.length, 0 is used.end >= array.length or end is omitted, array.length is used, causing all indices until the end to be filled.end is positioned before or at start after normalization, no index is filled.The modified array, filled with value.
The fill() method is a mutating method. It does not alter the length of this, but it will change the content of this.
The fill() method fills empty slots in sparse arrays with value as well.
The fill() method is generic. It only expects the this value to have a length property. Although strings are also array-like, this method is not suitable to be applied on them, as strings are immutable.
Note: Using Array.prototype.fill() on an empty array (length = 0) would not modify it as the array has nothing to be modified. To use Array.prototype.fill() when declaring an array, make sure the array has non-zero length. See example.
console.log([1, 2, 3].fill(4)); // [4, 4, 4] console.log([1, 2, 3].fill(4, 1)); // [1, 4, 4] console.log([1, 2, 3].fill(4, 1, 2)); // [1, 4, 3] console.log([1, 2, 3].fill(4, 1, 1)); // [1, 2, 3] console.log([1, 2, 3].fill(4, 3, 3)); // [1, 2, 3] console.log([1, 2, 3].fill(4, -3, -2)); // [4, 2, 3] console.log([1, 2, 3].fill(4, NaN, NaN)); // [1, 2, 3] console.log([1, 2, 3].fill(4, 3, 5)); // [1, 2, 3] console.log(Array(3).fill(4)); // [4, 4, 4] // A single object, referenced by each slot of the array: const arr = Array(3).fill({}); // [{}, {}, {}] arr[0].hi = "hi"; // [{ hi: "hi" }, { hi: "hi" }, { hi: "hi" }]
This example shows how to create a matrix of all 1, like the ones() function of Octave or MATLAB.
const arr = new Array(3); for (let i = 0; i < arr.length; i++) { arr[i] = new Array(4).fill(1); // Creating an array of size 4 and filled of 1 } arr[0][0] = 10; console.log(arr[0][0]); // 10 console.log(arr[1][0]); // 1 console.log(arr[2][0]); // 1
This example shows how to populate an array, setting all elements to a specific value. The end parameter does not have to be specified.
const tempGirls = Array(5).fill("girl", 0);
Note that the array was initially a sparse array with no assigned indices. fill() is still able to fill this array.
The fill() method reads the length property of this and sets the value of each integer-keyed property from start to end.
const arrayLike = { length: 2 }; console.log(Array.prototype.fill.call(arrayLike, 1)); // { '0': 1, '1': 1, length: 2 }
| Desktop | Mobile | Server | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | Deno | Node.js | ||
fill |
45 | 12 | 31 | 32 | 8 | 45 | 31 | 32 | 8 | 5.0 | 45 | 1.0 | 4.0.0 | |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill