JavaScript Array fill() Method Last Updated : 14 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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 ExamplesExample 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(); Output[ 87, 87, 87, 87 ] 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(); Output[ 1, 87, 87, 58 ] 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: Google Chrome 45.0Microsoft Edge 12.0Mozilla Firefox 31.0Safari 7.1Opera 32.0We 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. Comment More infoAdvertise with us Next Article JavaScript Array fill() Method H HGaur Follow Improve Article Tags : Misc JavaScript Web Technologies javascript-array JavaScript-Methods +1 More Practice Tags : Misc 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 ArrayDeque add() Method in Java The Java.util.ArrayDeque.add(Object element) method in Java is used to add a specific element at the end of the Deque. The function is similar to the addLast() method of ArrayDeque in Java. Syntax: Array_Deque.add(Object element) Parameters: The parameter element is of the type ArrayDeque and refers 2 min read Array to Stream in Java Prerequisite : Stream In Java Using Arrays.stream() : Syntax : public static <T> Stream<T> getStream(T[] arr) { return Arrays.stream(arr); } where, T represents generic type. Example 1 : Arrays.stream() to convert string array to stream. Java // Java code for converting string array // t 3 min read IntBuffer array() method in Java The array() method of java.nio.IntBuffer Class is used to Return the int array that backs this buffer. Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa. Invoke() the hasArray() method are used before invoking this method in order to ensure 3 min read ArrayDeque size() Method in Java The Java.util.ArrayDeque.size() method in Java is used to get the size of the Deque or the number of elements present in the Deque. Syntax: Array_Deque.size() Parameters: The method does not take any parameter. Return Value: The method returns the size or the number of elements present in the Deque. 2 min read Like