Array.prototype.fill
Changes all elements within a range of indices in an array to a static value, returning the modified array
Syntax
array.fill(value, start?, end?)Parameters
| Parameter | Type | Description |
|---|---|---|
| value | T | Value to fill the array with |
| start | number | Start index, default 0 |
| end | number | End index (exclusive), default array.length |
Return Value
The modified array, filled with value
Examples
const arr = [1, 2, 3, 4];
arr.fill(0, 2, 4);
console.log(arr); // [1, 2, 0, 0]const zeros = new Array(5).fill(0);
console.log(zeros); // [0, 0, 0, 0, 0]const grid = Array.from({ length: 3 }, () => new Array(3).fill(0));
console.log(grid); // [[0,0,0],[0,0,0],[0,0,0]]Understanding Array.prototype.fill
The Array.prototype.fill method in JavaScript changes all elements within a range of indices in an array to a static value, returning the modified array. It belongs to the Array object and is one of the most widely used methods for working with array values in modern JavaScript and TypeScript applications.
The method signature is array.fill(value, start?, end?). It accepts 3 parameters: value, start, end. When called, it returns the modified array, filled with value. Understanding when and how to use fill() helps you write more expressive, readable code.
Common use cases for Array.prototype.fill include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like array-copywithin, array-from, array-splice, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Array.prototype.fill is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
Array.prototype.copyWithinShallow copies part of an array to another location in the same array and returns it without modifying its length
Array.fromCreates a new, shallow-copied Array instance from an iterable or array-like object
Array.prototype.spliceChanges the contents of an array by removing or replacing existing elements and/or adding new elements in place
More Array Methods
Other methods in the Array object
Related Tools
More Array Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.