JavaScript Array fill() Method
Last Updated :
14 Mar, 2024
The fill()
method in JavaScript is used to fill all the elements of an array from a start index to an end index with a static value.
It mutates the original array and returns the modified array.
Syntax:
arr.fill(value, start, end)
Parameters:
This method accepts three parameters as described below:
Parameter
| Description
|
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 the 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.
JavaScript Array fill() Method Examples
Example 1: Filling Array with a Specified Value using JavaScript's fill() Method
The function initializes an array with values. It then uses fill()
to replace all elements with the value 87
. The modified array [87, 87, 87, 87]
is logged to the console.
JavaScript
// JavaScript code for fill() method
function func() {
let arr = [1, 23, 46, 58];
// fill array with 87
arr.fill(87);
console.log(arr);
}
func();
Example 2: Custom Range Filling with JavaScript's fill() Method
The function initializes an array with values. Using fill()
, it replaces elements from index 1 to 3 (exclusive) with the value 87. The modified array [1, 87, 87, 58]
is logged to the console.
JavaScript
// JavaScript code for fill() method
function func() {
let arr = [1, 23, 46, 58];
// here value = 87, start index=1 and
// and last index = 3
arr.fill(87, 1, 3);
console.log(arr);
}
func();
We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.
Supported Browsers: The browsers supported by the JavaScript Array fill() method are listed below:
We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.
Similar Reads
Array set() method in Java The java.lang.reflect.Array.set() is an inbuilt method in Java and is used to set a specified value to a specified index of a given object array. Syntax Array.set(Object []array, int index, Object value) Parameter : array : This is an array of type Object which is to be updated. index : This is the
3 min read
JavaScript typedArray.fill() Method The typedArray.fill() is an inbuilt function in JavaScript which is used to fill a value to typedArray from a start index to end index. Syntax: typedarray.fill(value, start, end) Parameters: It takes three parameters that are specified below- value: It is the value to fill with typed array.start: It
1 min read
Array setInt() method in Java The java.lang.reflect.Array.setInt() is an inbuilt method in Java and is used to set a specified int value to a specified index of a given object array. Syntax: Array.setInt(Object []array, int index, int value) Parameter: array: This is an array of type Object which is to be updated. index: This is
3 min read
Array setChar() method in Java The java.lang.reflect.Array.setChar() is an inbuilt method in Java and is used to change a specified char value to a specified index of a given object array. Syntax: Array.setChar(Object []array, int index, char value) Parameter: This method takes three parameters: array: This is an array of type Ob
3 min read
ArrayDeque pop() Method in Java The Java.util.ArrayDeque.pop() method in Java is used to pop an element from the deque. The element is popped from the top of the deque and is removed from the same. Syntax: Array_Deque.pop() Parameters: The method does not take any parameters. Return Value: This method returns the element present a
2 min read
Array setShort() method in Java The java.lang.reflect.Array.setShort() is an inbuilt method in Java and is used to set a specified short value to a specified index of a given object array. Syntax: Array.setShort(Object []array,int index, short value) Parameters: This method takes 3 parameters: array: This is an array of type Objec
3 min read