DEV Community

harkirat singh
harkirat singh

Posted on

Initialize array of given size in Javascript

If you want to initialize an array that you don't need to iterate over -

Array(100) or new Array(100)

If you do need to iterate over it -

Array.from({length: 5}, (v, i) => i) 

Top comments (1)

Collapse
 
salyadav profile image
Saloni Yadav

Just an adder: If you want to initialise it with say, all 0s or some number.. You can also do new Array(100).fill(0)