Where does the array get created after the else statement?

Where does the array get created after the else statement?

My question is how does javascript know arr is an array? is calling unshift on a variable going to immediately turn that const into an array?

  **Your code so far**

// Only change code below this line
function countdown(n){
if (n > 1) {
  return [];
} else {
  const arr = countdown(n - 1);
  arr.unshift(n);
  console.log(arr)
  return arr;
}
}
// Only change code above this line
  **Your browser information:**

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

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

1 Like

We know that arr is an array because it is declared like this:

  const arr = countdown(n - 1);

The function countdown() always returns an array, so arr is an array.

2 Likes

thank you so much! that helps clear things up.

Iā€™m glad I could help. Happy coding!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.