Most efficient way to create a zero filled JavaScript array?

前端 未结 30 1593
花落未央
花落未央 2020-11-22 05:58

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

30条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 06:05

    The already mentioned ES 6 fill method takes care of this nicely. Most modern desktop browsers already support the required Array prototype methods as of today (Chromium, FF, Edge and Safari) [1]. You can look up details on MDN. A simple usage example is

    a = new Array(10).fill(0);
    

    Given the current browser support you should be cautious to use this unless you are sure your audience uses modern Desktop browsers.

提交回复
热议问题