I’ll give it a stab. Let’s see…
So we call the function
countdown(5), and the first thing it checks is, is
n less than one? If SO, create an empty array. In this case, it’s not, so it drops into the else section, and calls
countdown(4), and assigns the return value to the variable
arr. This part is key.
n=4 - so less than one? Nope. So drop to the else, and call
countdown(3). Do it again,
Finally, we call
countdown(0). At this point,
n=0. Is this less than one? Yup. So… we return an empty array. Where do we return that empty array? To the function just before this one - the function call
So now we’re unwrapping our recursive calls. Inside
arr is an empty array, and
n=1. The next thing we do here is
n to that empty array, and returning
arr (which is now
) to the next layer out - the function call
At this point,
n=2. Remember, we’re working our way back out of a whole bunch of nested function calls. So, still in the else branch of
arr=[2,1], and return that value to
arr.unshifts(3), causing its own internal
arr to be [3,2,1].
And we KEEP doing this, till we get back to our outermost function call,
countdown(5). At that point, we’ve gone from an empty array, to an array with a single value, prepending and prepending, till we get to
At this point, we do it one last time, making
arr=[5,4,3,2,1], and we return that to our
console.log(countdown(5) ) statement, displaying that final array.
Does that clear things up at all?