There is a small error in your code:
You are calling a recursive function from your i + 1 , but not your index + 1 .
This makes index not equal to the current index of the array, but to the value.
For example, when you passed [0, 1, 2] , your data is now [0, 1] , and you are going to insert 3 , you call loop(3 + 1) , index 4 goes out of the range of arrays. if (index === end) condition is not satisfied, and it is not displayed. for (var i = index; i < len; i++) does not work, and everything goes wrong.
It should be:
var len = 4; var end = 3; var data = []; var loop = function (index) { if (index === end) { console.log(data); return; } for (var i = index; i < len; i++) { data[index] = i; loop(index + 1);
Here is a working demo of JSFiddle .
Update:
Oh, now I see. For all combinations, you need the condition a[i] > a[i-1] . Just add a start variable that will keep the last inserted value to match this rule.
var len = 4; var end = 3; var data = []; var loop = function (start, index) { if (index === end) { document.body.innerHTML += "<br/>" + data; return; } for (var i = start; i < len; i++) {
Updated JSFiddle demo with passing argument .
You can check the previous value instead of passing the value as an argument if it looks wrong to you. The condition will look like this: "if this is the first number, start at 0; else - start at the next number after the previous one."
var start = (index === 0 ? 0 : data[index-1] + 1);
Updated JSFiddle demonstration with the start of calculation .