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)
};
2
  • 1
    The version you've shown probably offers the best performance. Commented Sep 26, 2015 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. Remember to read their question requirements as they are more strict than this site. Commented Sep 26, 2015 at 15:01

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

You should add the new operator: var x = new Int8Array(100);
@DannyFardyJhonstonBermúdez thanks! :) fixed

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.