How to use the fill() Method In Javascript

The fill() method in JavaScript is a method that is used to populate an array with a static value. It accepts two arguments: the first argument is the value that will be used to fill the array, and the second argument is the starting index from where the filling process will start.

The fill method is useful in situations where we need to initialize an array with a default value or when we need to reset the values of an array to a specific value.

Syntax:

const zeros = new Array(size_array).fill(item);

Example: In the given example, we are creating an array of 0s of size 5 using the fill method.

Javascript
const zeros = new Array(5).fill(0);
console.log("Zeros: " + zeros);

Output
Zeros: 0,0,0,0,0

Different ways to populate an array in JavaScript

Populating an array in JavaScript means initializing it with elements. This could involve adding values manually, dynamically generating values using loops or functions, or copying elements from another array. It’s a fundamental operation for creating and manipulating arrays in JavaScript programs.

These are the following ways to populate an array:

Table of Content

  • Using Array literal notation
  • Using a for loop
  • Using the fill() Method
  • Using the from() Method
  • Using the map() Method
  • Using Spread Operator

Similar Reads

Method 1: Using Array literal notation

In JavaScript, an array literal notation is a way of creating an array by listing its elements inside square brackets, separated by commas. Using the array literal notation is a convenient method of populating an array with initial values. You can add as many values as you want, and you can mix and match different types of values....

Method 2: Using a for loop

You can use a for loop to populate an array with elements. Determine how many elements you want in the array. This could be a fixed number or a variable that’s determined at runtime. Use a for loop to iterate over the desired number of elements, adding each one to the array using the push() method....

Method 3: Using the fill() Method

The fill() method in JavaScript is a method that is used to populate an array with a static value. It accepts two arguments: the first argument is the value that will be used to fill the array, and the second argument is the starting index from where the filling process will start....

Method 4: Using the from() Method

The from() method in JavaScript is a method of populating an array. It creates a new array instance from an array-like or iterable object. The from() method returns a new array instance that contains the elements of the original array-like or iterable object....

Method 5: Using the map() Method

The map() method in JavaScript is a higher-order function that is used to iterate over an array and return a new array with modified elements based on a given function. The map() method applies the callback function to each element of the array in order and creates a new array with the results. The original array is not modified....

Method 6: Using Spread Operator

The Spread operator allows an iterable to expand in places where 0+ arguments are expected. It is mostly used in the variable array where there is more than 1 value is expected. It allows us the privilege to obtain a list of parameters from an array....