JavaScript Array fill()
Syntax & Examples
Array.fill() method
The fill() method of the Array class in JavaScript fills all the elements of an array from a start index to an end index with a static value. This method modifies the original array.
Syntax of Array.fill()
There are 3 variations for the syntax of Array.fill() method. They are:
fill(value)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
value | required | The value to fill the array with. |
This method fills all the elements of an array with a static value.
Returns value of type Array
.
fill(value, start)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
value | required | The value to fill the array with. |
start | optional | The start index, defaults to 0. |
This method fills all the elements of an array from the start index with a static value.
Returns value of type Array
.
fill(value, start, end)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
value | required | The value to fill the array with. |
start | optional | The start index, defaults to 0. |
end | optional | The end index, defaults to the array's length. |
This method fills all the elements of an array from the start index to the end index with a static value.
Returns value of type Array
.
✐ Examples
1 Using fill() method to fill an entire array
In JavaScript, we can use the fill() method to fill all the elements of an array with a static value.
For example,
- We define an array variable arr with elements [1, 2, 3, 4, 5].
- We use the fill() method with the value 0 to fill the entire array with 0.
- The resulting array is modified in place.
- We log the array to the console using console.log() method to see the filled array.
JavaScript Program
const arr = [1, 2, 3, 4, 5];
arr.fill(0);
console.log(arr);
Output
[0, 0, 0, 0, 0]
2 Using fill() method to fill an array from a start index
We can use the fill() method to fill elements of an array from a specified start index with a static value.
For example,
- We define an array variable arr with elements ['a', 'b', 'c', 'd', 'e'].
- We use the fill() method with the value 'x' and the start index 2 to fill the array from index 2 with 'x'.
- The resulting array is modified in place.
- We log the array to the console using console.log() method to see the filled array.
JavaScript Program
const arr = ['a', 'b', 'c', 'd', 'e'];
arr.fill('x', 2);
console.log(arr);
Output
['a', 'b', 'x', 'x', 'x']
3 Using fill() method to fill an array from a start index to an end index
We can use the fill() method to fill elements of an array from a specified start index to an end index with a static value.
For example,
- We define an array variable numArr with elements [1, 2, 3, 4, 5].
- We use the fill() method with the value 9, the start index 1, and the end index 3 to fill the array from index 1 to 3 with 9.
- The resulting array is modified in place.
- We log the array to the console using console.log() method to see the filled array.
JavaScript Program
const numArr = [1, 2, 3, 4, 5];
numArr.fill(9, 1, 3);
console.log(numArr);
Output
[1, 9, 9, 4, 5]
Summary
In this JavaScript tutorial, we learned about fill() method of Array: the syntax and few working examples with output and detailed explanation for each example.