JavaScript Array fill()
Examples
Fill all the elements with a value:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.fill("Kiwi");
Try it Yourself »
fruits.fill("Kiwi");
Fill the last two elements:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.fill("Kiwi", 2, 4);
Try it Yourself »
fruits.fill("Kiwi", 2, 4);
Description
The fill()
method fills specified elements in an array with a value.
The fill()
method overwrites the original array.
Start and end position can be specified. If not, all elements will be filled.
Syntax
array.fill(value, start, end)
Parameters
Parameter
Description
value
Required.
The value to fill in.
The value to fill in.
start
Optional.
The start index (position).
Default is 0.
The start index (position).
Default is 0.
end
Optional.
The stop index (position).
Default is array length.
The stop index (position).
Default is array length.
Return Value
Type
Description
Array The filled array.
Array Tutorials:
Browser Support
fill()
is an ECMAScript6 (ES6 2015) feature.
JavaScript 2015 is supported in all browsers since June 2017:
Chrome 51 |
Edge 15 |
Firefox 54 |
Safari 10 |
Opera 38 |
May 2016 | Apr 2017 | Jun 2017 | Sep 2016 | Jun 2016 |