Javascript Declare And Initialize Js Array

[Solved] Javascript Declare And Initialize Js Array | Perl - Code Explorer | yomemimo.com
Question : javascript initialize array

Answered by : exuberant-eland-k07948nw359u

const array = new Array(5).fill(0);
console.log(array);
// [0, 0, 0, 0, 0]

Source : https://attacomsian.com/blog/javascript-array-populate | Last Update : Mon, 13 Dec 21

Question : Declare and Initialize Arrays in javascript

Answered by : poised-peacock-8kfifgk36e8r

const array = Array(5).fill('');
// Output
(5) ["", "", "", "", ""]
const matrix = Array(5).fill(0).map(()=>Array(5).fill(0));
// Output
(5) [Array(5), Array(5), Array(5), Array(5), Array(5)]
0: (5) [0, 0, 0, 0, 0]
1: (5) [0, 0, 0, 0, 0]
2: (5) [0, 0, 0, 0, 0]
3: (5) [0, 0, 0, 0, 0]
4: (5) [0, 0, 0, 0, 0]
length: 5

Source : https://dev.to/devsimc/javascript-best-practices-44ll | Last Update : Mon, 25 Jul 22

Answers related to javascript declare and initialize js array

Code Explorer Popular Question For Perl