I have confusion on function recursion
function foo(i) {
console.log("checking: " + i);
if (i < 0)
return;
console.log('begin: ' + i);
foo(i - 1);
console.log('end: ' + i);
}
foo(1);
why is the output:
checking: 1
begin: 1
checking: 0
begin: 0
checking: -1
end: 0
end: 1
it should not print the end: 0, end: 1, because we are returning in if condition. What is happening?