# Use Recursion to Create a Range of Numbers; I'm not sure exactly how my code works

I’ve completed the challenge but I have a few questions about how the code works.

``````function rangeOfNumbers(startNum, endNum) {
if (startNum > endNum){
return [];
}else {
var rangeArray = rangeOfNumbers(startNum + 1, endNum);
rangeArray.unshift(startNum);
return rangeArray;
}
}

console.log(rangeOfNumbers(1,5))
``````
1. Why .unshift and not .push? I initially had .push thinking it would put 1, then 2 to the right of 1, then 3 to the right of 2, but it seems backwards and I don’t understand why.

2. How am I getting the first number? in `var rangeArray` I already added 1 to startNum and then I return it at the end. So how am I getting the number before the addition happens?

Sorry if the answers are obvious but I thought about it for a while and couldn’t figure it out.

try looking at it with this tool:
http://pythontutor.com/javascript.html

you can also try with these console.log statements, see if it helps or confund more.

``````function rangeOfNumbers(startNum, endNum) {
console.log({status: "start function", startNum, endNum});
if (startNum > endNum){
console.log("startNum > endNum is true");
console.log({status: "end function", returned: []});
return [];
}else {
console.log("startNum > endNum is false");
var rangeArray = rangeOfNumbers(startNum + 1, endNum);
console.log({rangeArray})
console.log("unshift startNum to rangeArray");
rangeArray.unshift(startNum);
console.log({status: "end function", returned: rangeArray});
return rangeArray;
}
}
``````

I honestly have more questions now lol. In addition to my previous questions:

1. The function seems to go between the if statement and `var rangeArray`, why? I thought it would go through the entire else statement before going back to the if.

2. It looks like it’s working through the array backwards, and the way it shows the unshift in both examples also seems backwards. I just can’t seem to get it.

I don’t mean to take up a lot of your time but I would appreciate if you could answer any of my questions. Tysm.