0

I want an array with N zeroes, like

[0,0,0,0...0]

I could do a for-loop and push them all, but is there a nicer way?

var x = []
for (var i = 0; i < N; i++) {
    x.push(0)
};
Rob
  • 14,746
  • 28
  • 47
  • 65
Himmators
  • 14,278
  • 36
  • 132
  • 223
  • 1
    The version you've shown probably offers the best performance. – Etheryte Sep 26 '15 at 14:59
  • It seems that your code currently works, and you are looking to improve it. Generally these questions are too opinionated for this site but you might find better luck at [CodeReview.SE](http://codereview.stackexchange.com/tour). Remember to read [their question requirements](http://codereview.stackexchange.com/help/on-topic) as they are more strict than this site. – Kyll Sep 26 '15 at 15:01

1 Answers1

3

Use Int8Array:

var x = new Int8Array(100)
# x now is filled with 0s and is of size 100

The Int8Array typed array represents an array of twos-complement 8-bit signed integers. The contents are initialized to 0.

See also: http://www.ecma-international.org/ecma-262/6.0/#table-49

dimakura
  • 7,575
  • 17
  • 36