Array fill()
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.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 45 | 12 | 31 | 8 | 45 | 8 | |
| Built-in object | ||||||
| The fill() method of TypedArray instances changes all elements within a range of indices in a typed array to a static value. It returns the modified typed array. This method has the same algorithm as Array.prototype.fill(). | 45 | 12 | 37 | 10 | 45 | 10 |
1+Supported (version) Not supported ※Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)
Syntax
JAVASCRIPT
// Initializing an array
new Array(5).fill(0); // [0, 0, 0, 0, 0]
// Partially filling an array
[1, 2, 3, 4, 5].fill(9, 2, 4); // [1, 2, 9, 9, 5] Live demo
range specifiedfill
Startposition and endposition specified, specific. partialonly write..
PreviewFullscreen
Use cases
-
Using Array fill()
The fill() method of Array instances changes all elements within a range of indices in an array to a static value.
Cautions
- No specific concerns. Stable across all major browsers.
Accessibility
- When updating the DOM dynamically, announce important changes to assistive technology with aria-live regions.