JavaScript | Array fill() function
Array.fill() function 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)
Here arr is the array to be filled with the static value.
Arguments
This function takes three arguments.
- value
- start (Optional)
- end (Optional)
It defines the static value with which the array elements are to be replaced.
it defines the starting index from where the array is to be filled with the static value. If this value is not defined the the starting index is taken as 0. If start is negative then the net start index is length+start.
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 function does not return a new array. Instead of it modifies the array on which this function is applied.
Examples for the above function are defined as follows:
Example 1:
var arr = [1, 23, 46, 58]; arr.fill(87);
Output:
[87, 87, 87, 87]
In this example the function fill() fills the entire array with 87, replacing all the initial values present in the array.
Example 2:
var arr = [1, 23, 46, 58]; arr.fill(87, 1, 3);
Output:
[1, 87, 87, 58]
In this example the function 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.
Example 3:
var arr = [1, 23, 46, 58]; arr.fill(87, 2);
Output:
[1, 23, 87, 87]
In this example the function fill() fills the array from index 1 to 3 with 87, replacing all the initial values present in the array.
Codes for the above function are defined as follows:
Program 1:
<script> // JavaScript code for fill() function 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]
Program 2:
<script> // JavaScript code for fill() function 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]
Program 3:
<script> // JavaScript code for fill() function 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]
Recommended Posts:
- JavaScript | Array.fill() Method
- JavaScript | typedArray.fill()
- p.js | fill() Function
- JavaScript | Array.of() function
- JavaScript | Array every() function
- JavaScript | Array some() function
- JavaScript | array.toLocaleString() function
- JavaScript | Array.prototype.map() function
- JavaScript | Array join() function
- JavaScript | Array findIndex() function
- How to pass a PHP array to a JavaScript function?
- JavaScript | array.includes() function
- JavaScript | Array find() function
- How to call a function that return another function in JavaScript ?
- JavaScript | Symbol.for() function
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.
Improved By : nidhi_biet



