I'm trying to learn recursion in Javascript and wrote this to trace a simple recursive function.
function count(num){
console.log(num + " top");
if (num===5)
return console.log("It has ended.");
count(num+1);
console.log(num + " bottom");
}
count(1);
Here's the output:
1 top
2 top
3 top
4 top
5 top
It has ended.
4 bottom
3 bottom
2 bottom
1 bottom
So what's happening here? Is there something wrong with my base condition? Is the log showing what's put on and taken off the stack? I would have expected the function to stop at "It has ended" and I'm not sure why it doesn't.
Thanks.