Javascript – Most efficient way to create a zero filled JavaScript array

arraysjavascript

What is the most efficient way to create an arbitrary length zero filled array in JavaScript?

Best Answer

ES6 introduces Array.prototype.fill. It can be used like this:

new Array(len).fill(0);

Not sure if it's fast, but I like it because it's short and self-describing.

It's still not in IE (check compatibility), but there's a polyfill available.