JavaScript: Use of Recursion, unshift vs push

Tell us what’s happening:
The code is fine, I just don’t understand why, to create an array with increasing numbers, push is used instead of unshift. Why is push happens lastly?

Your code so far


function rangeOfNumbers(startNum, endNum) {
if (startNum === endNum) {
  return [endNum];
} else  {
  var arr = rangeOfNumbers(startNum, endNum - 1);
  arr.push(endNum);
  return arr;
}

};

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36.

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-range-of-numbers

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

you will notice that the functions start having a return value (so a value is actually assigned to arr) only after return [endNum] is executed
at that point arr has its first value
if the function call is rangeOfNumbers(6,9) then the first time arr has value [6]
at that point, the next number is pushed to the array, so it becomes [6,7]
so that is the value returned
and arr is again assigned a value, [6,7], so new value is pushed, and becomes [6,7,8] and so on
try looking at it with that tool

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

Thanks for introducing the tool, it has been very helpful.
I will pay more attention about the spoiler problem, thank you.