JavaScript fill()Method

Array 对象参考手册JavaScript Array Object

Examples

Fill the array with a fixed value:

var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.fill("Example");

fruitsOutput result:

Example,Example,Example,Example

Try it yourself »

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 specify the first browser version that fully supports the 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 isarray.length)

Technical Details

Return value: Array
JavaScript Version: ECMAScript 6

More Examples

Examples

Fill the last two elements of the array with "Example":

var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.fill("Example", 2, 4);

Output result:

Banana,Orange,Example,Example

Try it yourself »

Array 对象参考手册JavaScript Array Object

Other extensions