ES6 - Array.fill


Advertisements

This function fills all the elements of an array from a start index to an end index with a static value. It returns the modified array.

Syntax

The syntax given herewith is for the array method fill(), where, −

  • value − Value to fill an array.

  • start − This is optional; start index, defaults to 0.

  • end − This is optional; end index, defaults to this length.

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

Example

<script>
   //fill
   let nosArr = [10,20,30,40]
   console.log(nosArr.fill(0,1,3))// value ,start,end
   //[10,0,0,40]

   console.log(nosArr.fill(0,1)) // [10,0,0,0]
   console.log(nosArr.fill(0))
</script>

The output of the above code will be as shown below −

[10, 0, 0, 40]
[10, 0, 0, 0]
[0, 0, 0, 0]
Advertisements