JavaScript Array fill() method
fills specified elements in an array with a static value

fill() method fills specified elements in an array with a static value.

syntax

fill(value, start, end)

value Value to fill the array with. Note: all filled elements in the array will be this exact value.

start index at which to start filling. Default is 0.

end index at which to end filling. Default is array length.

fill() method overwrites the original array.

fill() method fills empty slots in sparse arrays with value as well.

fill() returns the modified array.

Start and end position can be specified. If not, all elements will be filled.

The first position is 0, the second is 1, ...

A negative number selects from the end of the array.

To create an array with five slots and populate it with the string 'hello' ↴

new Array(5).fill('hello'); returns ↴

['hello', 'hello', 'hello', 'hello', 'hello']

syntax 1 description
fill all elements with value in myArray
myArray.fill(value)
syntax 2 description
fill all elements with value from start index to end of myArray
myArray.fill(value, start)
syntax 3 description
fill all elements with value from start index to end index (end not included) of myArray
myArray.fill(value, start, end)