JavaScript | Array.fill() Method
Fills given range of array elements with given value.
Syntax:
arr.fill(value[, start[, end]])
Parameters:
- Value:- Value to be filled.
- Start:- Start index (included) and its defaults value is 0.
- End:- End index (excluded) and its default index is this.length.
Return value:
It return the modified array.
Browser Support:
Here in 2nd column int values is the version of the corresponding feature browser.
<script> // input array contain some elements. var array = [ 1, 2, 3, 4 ]; // Here array.fill function fills with 0 // from position 2 till position 3. console.log(array.fill(0, 2, 4)); </script> |
Output:
> Array [1, 2, 0, 0]
Program 2:
Let’s see JavaScript program:
<script> // input array contain some elements. var array = [ 1, 2, 3, 4 ]; // Here array.fill function fill with // 9 from position 2 till end. console.log(array.fill(9, 2)); </script> |
Output:
> Array [1, 2, 9, 9]
Program 3:
Let’s see JavaScript program:
<script> // input array contain some elements. var array = [ 1, 2, 3, 4 ]; // Here array.fill function fill with // 6 from position 0 till end. console.log(array.fill(6)); </script> |
Output:
> Array [6, 6, 6, 6]