Use Recursion to Create a Countdown - returning empty array without declaration?

Hi,
I have a question in regards to the example in the lesson Use Recursion to Create a Countdown under Javascript .

How are you able to return an empty array that is not declared yet?
As far as order of operation?
Please correct me if I’m wrong, but I think the empty array is the const countArray, but again, how can return it first without declaration.
Because I thought it was stressed early on that Javascript works top down, and that order matters as far as to what comes first and the like.

Hopefully my question makes since. I’m just trying my best to understand Javascript and not just guess at things or assume, but rather actually understand and know what I’m doing.

Thanks,
Shawn Wright

function countup(n) {
  if (n < 1) {
    return [];
  } else {
    const countArray = countup(n - 1);
    countArray.push(n);
    return countArray;
  }
}
console.log(countup(5));

So, I see where you are coming from but you have a small misunderstanding. You don’t declare an array. You declare variables. When you declare a variable, you may initialize the variable with data. That data might be an empty array. But you can also use that data on its own wherever you need. You don’t need to declare a variable holding 0 every time you want to use 0 - same thing with an empty array.

1 Like

I understand now.
Thank you for clearing that up @JeremyLT

1 Like