JavaScript fill() Method
Example
Fill an array with a fixed value:
var
fruits
=
[
"
Banana
"
,
"
Orange
"
,
"
Apple
"
,
"
Mango
"
]
;
fruits
.
fill
(
"
Runoob
"
)
;
fruits Output result:
Runoob,Runoob,Runoob,Runoob
Try it »
Definition and Usage
The fill() method is used to replace the elements of an array with a fixed value.
Browser support
The numbers in the table indicate the first browser version that supports this method.
| Method | |||||
|---|---|---|---|---|---|
| fill() | 45.0 | 12.0 | 31.0 | 7.1 | 32.0 |
Note: IE 11 and earlier versions do not support the fill() method.
Syntax
array.fill(value, start, end)
Parameters
| Parameters | Description |
|---|---|
| value | Required. The value to fill with. |
| start | Optional. The position to start filling. |
| end | Optional. The position to stop filling (default is array.length) |
Technical Details
| Return value: | arrays |
|---|---|
| JavaScript version: | ECMAScript 6 |
More examples
Example
Fill "Runoob" into the last two elements of the array:
var
fruits
=
[
"
Banana
"
,
"
Orange
"
,
"
Apple
"
,
"
Mango
"
]
;
fruits
.
fill
(
"
Runoob
"
,
2
,
4
)
;
Output result:
Banana,Orange,Runoob,Runoob
Try it »
JavaScript Array object