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
ArrayThe filled array.


Browser Support

fill() is an ECMAScript6 (ES6) feature.

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

Chrome 51 Edge 15 Firefox 54 Safari 10 Opera 38
May 2016 Apr 2017 Jun 2017 Sep 2016 Jun 2016

fill() is not supported in Internet Explorer.


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.