Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2015.
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.
Value 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 OptionalZero-based index at which to start filling, converted to an integer.
Zero-based index at which to end filling, converted to an integer. fill() fills up to but not including end.
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.
This example shows how to create a matrix of all 1, like the ones() function of Octave or MATLAB.
This example shows how to populate an array, setting all elements to a specific value. The end parameter does not have to be specified.
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.
| ECMAScript® 2027 Language Specification # sec-array.prototype.fill |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Jul 20, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.