The fill() Function is used to fill the array locations with a specified value.
Let us say we have the below array with four elements.
var x = [5, 8, 2, 9]
And we want to fill 2nd and 3rd location with 100.
<html> <body> <script> var x = [5, 8, 2, 9] var y = x.fill(100, 1, 3) document.write(y) </script> </body> </html>
So, in the above code we have array that has 4 numbers.
var x = [5, 8, 2, 9]
Next, when the fill() Function is called. It accepts 3 arguments.
The first argument is the value to be substituted with i.e. 100.The second argument is the index of the array that should be substituted with the value i.e. 1.The third argument is the last index plus 1 i.e. 3.
var y = x.fill(100, 1, 3)
And we get the below output.