JavaScript Array fill() Method

12 Oct, 2021
Below is the example of the Array fill() method to fill with a single digit.  
Example:  
JavaScript
<script>    // JavaScript code for fill() method    function func() {        var arr = [1, 23, 46, 58];         // fill array with 87        arr.fill(87);        document.write(arr);    }    func();</script>
Output:  
[87, 87, 87, 87]
The arr.fill() method is used to fill the array with a given static value. The value can be used to fill the entire array or it can be used to fill a part of the array. Syntax:  
arr.fill(value, start, end)
Parameters: This method accepts three parameters as mentioned above and described below:  
value: It defines the static value with which the array elements are to be replaced.
start (Optional): It defines the starting index from where the array is to be filled with the static value. If this value is not defined the starting index is taken as 0. If start is negative then the net start index is length+start.
end (Optional): This argument defines the last index up to which the array is to be filled with the static value. If this value is not defined then by default the last index of the i.e arr.length – 1 is taken as the end value. If the end is negative, then the net end is defined as length+end.
Return value: This method does not return a new array. Instead of it modifies the array on which this method is applied.Below examples illustrate the Array fill() method in JavaScript: 
Example 1: In this example the method fill() fills the entire array with 87, replacing all the initial values present in the array. 
var arr = [1, 23, 46, 58];
arr.fill(87); 
Output:  
[87, 87, 87, 87]
Example 2: In this example the method fill() fills the array from index 1 to 2 one less than the upper index with 87, replacing all the initial values present in the array.  
var arr = [1, 23, 46, 58];
arr.fill(87, 1, 3); 
Output:  
[1, 87, 87, 58]
Example 3: In this example the method fill() fills the array from index 1 to 3 with 87, replacing all the initial values present in the array. 
var arr = [1, 23, 46, 58];
arr.fill(87, 2); 
Output:  
[1, 23, 87, 87]
Codes for the above method are defined as follows:Program 1:  
JavaScript
<script>    // JavaScript code for fill() method    function func() {        var arr = [1, 23, 46, 58];         // Here value = 87, start index=1        arr.fill(87, 2);        document.write(arr);    }     func();</script>
Output:  
[1, 23, 87, 87]
Program 2:  
JavaScript
<script>    // JavaScript code for fill() method    function func() {        var arr = [1, 23, 46, 58];         // here value = 87, start index=1 and        // and last index = 3        arr.fill(87, 1, 3);        document.write(arr);    }     func();</script>
Output:  
[1, 87, 87, 58]
Supported Browsers: The browsers supported by JavaScript Array fill() method are listed below:  
Google Chrome 45.0
Microsoft Edge 12.0
Mozilla Firefox 31.0
Safari 7.1
Opera 32.0
 
nidhi_biet
Akanksha_Rai
as5853535
sumitgumber28
javascript-array
JavaScript-Methods
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Difference Between PUT and PATCH Request
How to Open URL in New Tab using JavaScript ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?