How to use the Array.from() In Javascript
Use the following syntax to fill the array of size n with 0 value. Use any value in place of 0.
Syntax:
Array.from({length: n}, ()=> 0 );
let arr = Array.from({length: 5}, () => 0);
console.log(arr.length);
console.log(arr)
Output:
5
[ 0, 0, 0, 0, 0 ]
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.