Array.prototype.fill
The fill() method changes all elements in an array to a static value, from a start index (default 0) to an end index (default array.length).
It returns the modified array.
Syntax
fill(value)
fill(value, start)
fill(value, start, end)
Parameters
value- : Value to fill the array with. Note all elements in the array will be this exact value: if
valueis an object, each slot in the array will reference that object.
- : Value to fill the array with. Note all elements in the array will be this exact value: if
startoptional- : Zero-based index at which to start filling, converted to an integer.
- Negative index counts back from the end of the array — if
start < 0,start + array.lengthis used. - If
start < -array.lengthorstartis omitted,0is used. - If
start >= array.length, no index is filled.
- Negative index counts back from the end of the array — if
- : Zero-based index at which to start filling, converted to an integer.
endoptional- : Zero-based index at which to end filling, converted to an integer.
fill()fills up to but not includingend.- Negative index counts back from the end of the array — if
end < 0,end + array.lengthis used. - If
end < -array.length,0is used. - If
end >= array.lengthorendis omitted,array.lengthis used, causing all indices until the end to be filled. - If
endis positioned before or atstartafter normalization, no index is filled.
- Negative index counts back from the end of the array — if
- : Zero-based index at which to end filling, converted to an integer.
Return value
The modified array, filled with value.
Description
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.