Where is the array?

In Solution 1 using if/else, where is the array ‘numbers’ actually created? It is shown as a variable assignment so i don’t understand.

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36.

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

I have added [spoiler] tags for those who haven’t worked on the problem yet.

The array is created here

This is the base case in recursion.

Here is what is happening in the computer.

If my end number is 5 and the start number is 1 for example.
The computer will check the if statement first
Is 1 equal to 5? No.

So then it goes to the else statement
rangeOfNumbers(1, 5 - 1) or rangeOfNumbers(1, 4)

So then we start again
The computer will check the if statement first
Is 1 equal to 4? No.

Then we move to the else statement and keep repeating this process until we get to is 1 === 1? Yes

Then we return [1]

So now the numbers array is [1]

Then we execute these lines of code
numbers.push(2);
Numbers array is [1,2]
numbers.push(3);
Numbers array is [1,2,3]
numbers.push(4);
Numbers array is [1,2,3,4]
numbers.push(5);
Numbers array is [1,2,3,4,5]