New ES6 array extensions allow you to do this natively with fill
method. Now IE edge, Chrome and FF supports it, but check the compatibility table
new Array(3).fill(0)
will give you [0, 0, 0]
. You can fill the array with any value like new Array(5).fill('abc')
(even objects and other arrays).
On top of that you can modify previous arrays with fill:
arr = [1, 2, 3, 4, 5, 6]
arr.fill(9, 3, 5) # what to fill, start, end
which gives you: [1, 2, 3, 9, 9, 6]