-1

How to create an array with length 3 and the value for the array element is 0. if i give the length of the array as 3, then the array should be like below

  a[0]=0;
  a[1]=0;
  a[2]=0;

I have tried this new Array(3), it creates an array a[,,,] of length 3 and new Array(0,0,0) creates an array a[0,1,2], Is it possible to create an array by defining the length and value for the array element?

Thanks in Advance

user3326265
  • 257
  • 1
  • 4
  • 14
  • Some interesting answers about how to create a prefilled array [on this post](http://stackoverflow.com/questions/1295584/most-efficient-way-to-create-a-zero-filled-javascript-array). – Andy Jul 24 '14 at 10:58
  • 1
    _"and `new Array(0,0,0)` creates an array `a[0,1,2]`"_ - No it doesn't, it creates the `[0,0,0]` array you're looking for. – nnnnnn Jul 24 '14 at 11:10

1 Answers1

5

Is it possible to create an array by defining the length and value for the array element?

If you mean, some kind of pre-initializing constructor or fill operation, no, JavaScript doesn't have that.

If you know you want three elements as of when you're writing the code, you can do this:

a = [0, 0, 0];

It's called an array initializer.

If you don't know in advance (e.g., when writing the code) how big the array should be, you can just fill it as necessary:

a = [];
var index;
for (index = 0; index < initialSize; ++index) {
    a[index] = 0;
}

Note that you don't have to pre-allocate room in the array; the array can "grow" as required. (This may seem strange, but standard JavaScript arrays aren't really arrays at all, although JavaScript engines will frequently try to use true arrays as an optimization if they think they can.)

But if you want to, you can tell the engine engine in advance what length you want, by using:

a = new Array(initialSize);

...rather than a = []; above. (More on this near the end.)

If you wanted to, you could add a function to the Array function object to do that:

Array.createFilled(length, value) {
    var a = new Array(length); // Or of course, `var a = [];`
    var i;
    for (i = 0; i < length; ++i) {
        a[i] = value;
    }
    return a;
}

Then using it in the various places you want a pre-filled array:

var a = Array.createFilled(3, 0); // 0,0,0

Side note: Always be sure to declare your variables, e.g., have a var a; somewhere prior to the line above.


Obviously, telling the engine in advance how big the array should be would let it do better optimization, right? Not necessarily! It may help some engines (a small bit), hinder others (a small bit), and/or make no difference:

chart shorting jsperf results with Chrome fastest with no length, Firefox fastest with length in the constructor, and IE11 not caring

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Though that's implicitly given by your answer, i think it'd nice to point out that arrays do not need to be initialized with a specific length – Moritz Roessler Jul 24 '14 at 10:58
  • Side note: I would not recommend extending javascript basic function objects with own methods. There is chance of breaking some other functionality of those objects. (Something which I remember reading from book Effective JavaScript: 68 Specific Ways to Harness the Power of JavaScript). Also, I think that existence of those methods might be rather confusing or unknown to other devs as all they are seeing is a normal Array function object. – Mauno Vähä Jul 24 '14 at 15:26
  • @MaunoV.: It's more of an issue if you add things to the *prototypes* attached to things, such as `Array.prototype`. But yes, there's the possibility of collision if someone else adds something called `createFilled` to the `Array` constructor function object. My take is: It's fine to do this in your own code, it's not fine to do this in libraries others will use. But of course, an `App.Utils.createFilled` would work. :-) – T.J. Crowder Jul 24 '14 at 16:05