Now i was reading at MDN functions section, and i was reading this example about recursion, which was:
function foo(i) {
if (i < 0) //1
return;
console.log('begin:' + i); //2
foo(i - 1); //3
console.log('end:' + i); //4
}
foo(3);
So what I would normally expect of calling the recursive function foo with an argument of 3 is that it goes to line 1 checks that condition which is at the beginning not gonna hold so it continues to line 2 and logs the ("begin: 3") then it goes to line 3 where recursion happens and that will make it go back to the line 1 and go through the same flow of processes, which should lead to ("begin: 2"), ("begin: 1"), ("begin: 0"), then it should stop so that it won't clog ("begin: -1"), that indeed happens in MDN example but somehow it logs is one more time but reversibly like this:
// Output:
// begin:3
// begin:2
// begin:1
// begin:0
// end:0
// end:1
// end:2
// end:3
My Questions:
in which sense after that (i = -1) and the function returns, the function is not exiting the function and continue to line number 4?
how is (i) increasing again, i see no loop or recursion that is increasing the value of (i) again so that it logging again from 0 to 3??