How to use the spread operator In Javascript
In this example, the spread operator is used to expand the Array(5) which creates an array with 5 empty slots, and then the Array.map() method is used to fill all the empty slots with the value “Hello”
let myArray = new Array(5);
myArray = [...Array(5)].map(() => 'Hello');
console.log(myArray);
Output: ["Hello", "Hello", "Hello", "Hello", "Hello"]
How to fill an array with given value in JavaScript ?
In JavaScript, an array is a collection of elements that can be of any data type. Sometimes, it is necessary to fill an array with a specific value for a given number of times. For example, an array of length 10 needs to be filled with the value 0, or an array of length 5 needs to be filled with the value “Hello”.
Approach: There are several ways to fill an array with a given value in JavaScript.
Contact Us