function range(j, k) { return Array .apply(null, Array((k - j) + 1)) .map(function(_, n){ return n + j; }); }
it is roughly equivalent
function range(j, k) { var targetLength = (k - j) + 1; var a = Array(targetLength); var b = Array.apply(null, a); var c = b.map(function(_, n){ return n + j; }); return c; }
break it:
var targetLength = (k - j) + 1; var a = Array(targetLength);
this creates a sparse matrix with the correct nominal length. Now the problem with the sparse matrix is ββthat, although it has the correct nominal length, it does not have real elements, so for
j = 7, k = 13 console.log(a);
gives us
Array [ <7 empty slots> ]
Then
var b = Array.apply(null, a);
passes the sparse matrix as an argument list to the Array constructor, which creates a dense (actual) matrix length targetLength, where all elements are undefined. The first argument is the value of 'this' for the context of the execution of the array constructor function and does not play any role here, and therefore it is null.
So now
console.log(b);
gives
Array [ undefined, undefined, undefined, undefined, undefined, undefined, undefined ]
finally,
var c = b.map(function(_, n){ return n + j; });
uses the fact that the Array.map function passes: 1. the value of the current element and 2. the index of the current element, delegate / callback of the map. The first argument is discarded, and the second can be used to set the correct sequence value after setting the start offset.
So then
console.log(c);
gives
Array [ 7, 8, 9, 10, 11, 12, 13 ]