目录

JavaScript Array fill()

Examples

Fill all the elements with a value:

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.fill("Kiwi");
Try it Yourself »

Fill the last two elements:

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.fill("Kiwi", 2, 4);
Try it Yourself »

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.
start Optional.
The start index (position).
Default is 0.
end Optional.
The stop index (position).
Default is array length.

Return Value

Type Description
Array The filled array.


Browser Support

fill() is an ECMAScript6 (ES6) feature.

ES6 (JavaScript 2015) is supported in all modern browsers:

Chrome Edge Firefox Safari Opera
Yes Yes Yes Yes Yes

fill() is not supported in Internet Explorer 11 (or earlier).