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:
- Using Array fill() Method
- Using for loop
- Using push() method
- Using from() method
- Using spread operator