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.



filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


Output:

> Array [1, 2, 0, 0]

Program 2:
Let’s see JavaScript program:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


Output:

> Array [1, 2, 9, 9]

Program 3:
Let’s see JavaScript program:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


Output:

> Array [6, 6, 6, 6]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.