Foo Function from Reading

javascript

#1

from:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions
(about 40% down the page)

function foo(i) {
if (i < 0)
return;
console.log('begin:' + i);
foo(i - 1);
console.log('end:' + i);
}
foo(3);

// Output:

// begin:3
// begin:2
// begin:1
// begin:0
// end:0
// end:1
// end:2
// end:3

I follow all the way to end:0 output but can't understand how it goes to end:1, etc.....Any help here? Thanks in advance.


#2

Can you copy paste your code as it is? Cause the output here is wrong. 3 is not greater than 0 so it will never go to that if statement. Making it never run.

if it says

  else{
     foo(i-1)
      }

Thats another story.

If thats the case the output should be :

end2
end1 
end0
begin1
begin2... Never Ending

#3

I entered the link and indented the code as it's listed. thanks.