How to use spread operator In Javascript

We use the spread operator to fill static values in an array in JavaScript. The spread operator is denoted by three dots (…). The spread operator helps to copy all elements from an existing array to another array. Let’s look at an example.

Example:  In the following example, we use the spread operator to fill the array with 100 with the help of the map function.

Javascript




let arr = [1, 3, 5, 8, 9, 10];
let arr2 = [...arr].map((value) => value = 100);
console.log(arr2);


Output:

[ 100, 100, 100, 100, 100, 100 ]


How to fill static values in an array in JavaScript ?

In this article, we will see the methods to fill an array with some static values. There are many ways to fill static values in an array in JavaScript such as:

Similar Reads

Method 1: Array fill() Method

We use the array fill() method to fill static values in an array in JavaScript. This JavaScript array fill() method is used to fill an array with a given static value from the specified start position to the specified end position. If we do not specify the start and end position then the whole array will be filled up with a given static value. This fill() method modifies the original array and returns it....

Method 2: Using for loop

...

Method 3: Using push() method

...

Method 4: Using from() method

...

Method 5: Using spread operator

...