-1

I want to make a new array (of X elements) with all the elements set to 0.
X is set before.
Anyone could make the most compact and easy code for that? Thank you for your help.

1
  • @EdHeal Thanks for the negativity :D Commented Feb 13, 2015 at 19:48

1 Answer 1

1

Just create a function:

function makeArray(size, defaultValue) {
    var arr = [];
    for (var i = 0; i < size; i++) arr.push(defaultValue);
    return arr;
}

var myArr = makeArray(10, 0);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.