JavaScript Array fill() Method


The JavaScript array fill() method fills the elements of the given array with the specified static values. This method modifies the original array. It returns undefined, if no element satisfies the condition.

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

You can specify the position of where to start and end the filling. If not specified, all elements will be filled.

Syntax

array.fill(value, start, end)

Parameter

value - The static value to be filled.

start - It is optional. It represents the index from where the value starts filling. By default, it is 0.

end - It is optional. It represents the index where the value stops filling. By default, it is length-1.

Example

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

Example -