JavaScript Array fill() Method

Definition and Usage

fill() The method fills the specified elements in the array with a static value.

You can specify the start and end positions for filling. If not specified, all elements will be filled.

Note:fill() It will override the original array.

Example

Example 1

Fill all array elements with a static value:

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

Try It Yourself

Example 2

Fill the last two array elements with a static value:

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

Try It Yourself

Syntax

array.fill(value, start, end)

Parameter Value

Parameter Description
value Required. The value to be used to fill the array.
start Optional. The index at which to start filling the array (default is 0).
end Optional. The index at which to stop filling the array (default is array.length).

Technical Details

Return value: Array, the modified array.
JavaScript version: ECMAScript 6

Browser Support

The numbers in the table indicate the first browser version that fully supports this method.

Chrome Edge Firefox Safari Opera
Chrome 45 Edge 12 Firefox 31 Safari 7.1 Opera 32
September 2015 July 2015 July 2014 September 2014 September 2015

Note:Internet Explorer does not support fill() Methods.

Related Pages

Tutorial:JavaScript Array

Tutorial:JavaScript Array Const

Tutorial:JavaScript Array Methods

Tutorial:JavaScript Sorting Arrays

Tutorial:JavaScript Array Iteration