Create an Array with n values that is able to use Array methods instantly As it is impossible to use Array methods on new Array(n), e.g. new Array(5).map(/* [some callback] */), I deviced a pseudo static Array method.

The idea is to call an anonymous function that fills an empty Array with n times the value undefined. In its simplest form it returns an Array for which Array-methods are applicable. 

If you supply parameters (a callback method (Function), the method identifier itself (String, default = 'map') and optionally an initial value, the  callback method mapCB is applied to the (Array)method, and the resulting Array is returned. 

It looks like this:



window.addEventListener('load', function () {
    Array.create = function (n, mapCB, method, initial) {
                             method = method in [] ? method : 'map';
                             var nwArr = ( function (nn){
                                             while (nn--) 
                                               this.push(undefined); 
                                             return this; } )
                                           .call([],n);
                             return mapCB ? nwArr[method](mapCB, initial) : nwArr;
    };
    // usage examples
    var someArr0 = Array.create(100).map(function (v, i) {return i;});
    var someArr1 = Array.create(100, function (v, i) {return this + i;}, 'map', 5);
    var someArr2 = Array.create(100, function (p, n, i) {return p+i;}, 'reduce', 0);
    Helper.log2Screen('someArr0: [ &lt;code&gt;',someArr0.join(', '),'&lt;/code&gt; ]');
    Helper.log2Screen('someArr1: [ &lt;code&gt;',someArr1.join(', '),'&lt;/code&gt; ]');
    Helper.log2Screen('someArr2: &lt;code&gt;',someArr2,'&lt;/code&gt;');
});
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;
&lt;script src="http://www.nicon.nl/genericsvc/fiddle/fiddlemeta.js"&gt;&lt;/script&gt;




Do you think it's useful, or are there better way to achieve this?

I'm aware of the Array.apply(undefined, Array(n)) to create a usable Array, but that will run into a maximum call stack error at some point with a large n used.

Based on the comments and given answer, a more readable approach would be:

    function createArrayWithNElements(nElements, defaultValue) {
      var arr = [];
      for (var i=0; i &lt; nElements; i+=1) {
         arr.push(defaultValue);
      };
      return arr;
    }
    function createArrayWithNElementsAndApplyMethod(n, methodObj) {
      var arr = createArrayWithNElements(n);
      return arr[methodObj.method](methodObj.callback, methodObj.initialValue);
    }
    Array.create = createArrayWithNElements;
    Array.createAndApply = createArrayWithNElementsAndApplyMethod;
    // usage examples
    Array.create( 10, 0 ); //=&gt; [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    Array.createAndApply( 10, { method:'map', 
                                callback: function (v, i) {return this + i;},
                                initialValue: 5 } );
    //=&gt; [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
As the comments already say, I'd not put too much into such a function, that is, IMO creating the array with initial size and elements is probably enough to make a difference.

Array.create = function (n, initial) {
  var result = new Array (n);
  for (var i = 0; i &lt; n; ++i)
    result[i] = initial;
  return result;
};

// Array.create (3);
// Array.create (5, "hi").map (...);


Which also could just be a regular function makeArray or so I guess. So you get one benefit without the rather cryptic calling convention (mapCB/method) you have at the moment. Sure, it's a matter of opinion, but I had to take a moment to understand what that is supposed to do, so arguably that is not a good choice if you want other people to read that later.

And good point about the apply, I wouldn't want to see that in real code as well.