To understand recursion, you have to understand delegation.
The function countup is supposed to create an array which has the numbers 1 up to n (the input value)
It does it by worrying about one number at a time and delegating the rest of the work.
So let us say we have the number 3 as input.
First it will ask if 3 is less than 1.
It is not.
Then it will say: I have to delegate this big number! I can only handle one number at a time!
This is the line here: const countArray = countup(n - 1);
Which is essentially delegating the handling of n-1 (2 now) to another countup.
Then the current countup will wait for the next one to return.
The next countup gets the 2. And it asks, is 2<1, no.
So it does delegation also and calls yet another countup but with n-1 . Then it sits and waits
Now the third countup receives a 1
Is 1<1? No.
So again it delegates the result of n-1 (0) to a fourth countup! And waits…
The fourth countup sees a 0. Is 0 less than 1, yes! So it returns an empty array!!! Finally.
This empty array is given back to the third countup mentioned before. Remember it was waiting?
Now it has a response of an empty array so it pushes the number 1 to it (if you recall the third countup was given number 1 as input). Then it returns the result which is an array containing number 1.
Recall that the second countup was waiting around still? Now it is given the array and it adds its number to it so now we have [1,2] and then returns it.
Recall again the very first countup was waiting? Now it is given the array so it can add 3 to it (which was the original input to the first countup).
Thank you for the explanation. Now I think I understand this point. So every time, countArray.push(n) is waiting because the recursive function does not yet complete its task. And when it reaches its base condition, the const countArray returns with an array, and just after, the waiting countArray.push pushes the value of n. Is this function actually executing in this way? Please suggest to me whether I get the point or not.
a metaphor for this is an assembly line of people who need to make a tower of bricks. But each person can only add one.
So you give the first person you see the instruction of build a tower 5 bricks high. So he gets his brick ready but he needs to wait for the next person to add their brick etc until we are at the last person who adds his brick to the floor and then everyone can add theirs (one by one) on top.