Sometimes we need to fill an array with a specific value. There are many ways to do this, but the best and easiest way is to use the method. In the following example, we use this method to fill an empty array with numbers. In this example, we use the Javascript array method to fill an array that contains values with numbers. In the first array we filled only the first three values from the index to the index with the number , in the second array we filled the whole array with the number . What is the Javascript fill method used for? The method of changes all the elements in an array to a static value and returns a new array with the new values. This method has many use cases for example, you can use it to clear the values of an array or to initialize an array or a two dimensional matrix with default values. Basically, you can use this method whenever you want to change more than one of the elements of an array to a single specific value. This method receives three parameters: : this parameter is the value with which you want to fill the array, if this parameter is not passed the array will be filled with the value , you can pass any type of data as a value, it can be a , a , an , an , etc... : this is the index at which to start filling the array (optional). if this value is a negative number it starts from the backwards. if this value is greater than none of the values in the array are changed. : this is the index at which to end filling the array (optional). if this value is a negative number then it starts from the backwards. if this value is greater than then the value is used. This is an example of how to use these three parameters: Here we have an array filled with values and then we call the method to fill this array with the number starting at the index and ending but not including the value at the index , this method returns the modified array which is store in the constant . You'll have a better understanding of this method with these examples: Is important to remember that the method returns a reference to the original modified array as shown in the next example: In this code, we create an array with 5 elements. Then we call the method to fill all the elements in this array with the number and store the result in the constant . After calling the method both the original and the modified arrays have the same values, this is because the method changes the values of the original array and returns a reference to it which is store in the second array . So if you compare the two arrays the result will be because they both store a reference to the original modified array. Use cases of the Javascript fill method The Javascript method has multiple use cases, we'll see some of them in the next examples. 1. Initialized an array with elements The method can be used to initialize an array with elements by default. In this example, we use the method together with the method to initialize an array with numbers. First, we create an array with empty elements and then we call the method to fill the array with iterable elements. After that, we use the method to assign the index of each element plus one as its value. For example, the index of the first element is plus one equals so the value of the first element of the array will be , the index of the second element is plus one equals so the value of the second element will be , and so on with all the elements of the array. This way to initialize an array with elements is especially important when you need to create an array with a massive amount of data, for example, the even numbers from to . This example works the same as the previous one only that it uses the method to filter all the even numbers of the array, with the syntax . 2. Initialize an empty two dimensional matrix An interesting use for the Javascript method is to initialize a two dimensional matrix, for example, a matrix to play the game tic tac toe . output: newMatrix In this example we want to create a two dimensional matrix for the tic tac toe game. For this, we have to create an array with empty values with the syntax then we call the method to fill these three values with three new arrays with empty values with the syntax and then we call the method on each of this arrays with the value to initialized the two dimensional matrix for the game, we do this with the syntax . 3. Create mock data Another way you can use the method is to create mock information in an array. output: mockToDos In this example, we use the Javascript array method to create mock data for a To Do application. First, we create an array with 10 empty elements with the syntax then we call the method with an object as value, this object has an property which is an increasing number also another property called which is the task that you want to complete and finally the property which indicates whether you have already completed the task or not. Conclusion We can use the Javascript method for many things, to initialize an array with