Skip to content
Related Articles

Related Articles

Improve Article

JavaScript | Array.fill() Method

  • Last Updated : 10 Mar, 2018

Fills given range of array elements with given value.
Syntax:

arr.fill(value[, start[, end]])

Parameters:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :