Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
JS, Recursion, Function Stack Example, Hard to Understand

Below is an example from MDN Function scope. I can not understand how the end: 0 and the rest of end: are printed. I was expecting that the console.log("end: " + i); will never be reached.

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
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

If I were to write:

console.log("begin");
my_custom_function(1);
console.log("end")

Then console.log will log begin, then my_custom_function will be called, and when my_custom_function has finished it will pick up where it left off and console.log will log end.

It's exactly the same.

When foo has finished, it will return to the calling function (which happens to also be foo) and continue where it left off.

about 4 years ago · Juan Pablo Isaza Report

0

The trick to all recursion is the exit condition. Without it the function will run forever or the system runs out of memory. The exit condition for foo is if (i < 0) return;. Now because javascript is, in this case at least, run synchronously, it will finish whatever it is doing before moving on to the next line of code.

The function calls itself with foo(i - 1); which will in turn call itself and so on until the exit condition is met. It is therefore vital that the exit condition is placed before the recursive call.

To make this easier to understand, consider the value of i for each successive call:

foo(3)
  foo(2)
    foo(1)
      foo(0) // exit condition is met, ending the recursion
      print end 0
    print end 1
  print end 2
print end 3

about 4 years ago · Juan Pablo Isaza Report

0

Think about it in blocks. Below I added each step showing the actual i values and indenting it on each recursion. You can see that after the recursion is done, then it will finish the function call on each of the previous blocks.

foo(3) {
    if (3 < 0) return;
    console.log("begin: " + 3);
    foo(3-1) {
        if (2 < 0) return;
        console.log("begin: " + 2);
        foo(2-1) {
            if (1 < 0) return;
            console.log("begin: " + 1);
            foo(1-1) {
                if (0 < 0) return;
                console.log("begin: " + 0);
                foo(0-1){
                    if (-1 < 0) return; // Exit recursion
                }
                console.log("end: " + 0);
            }
            console.log("end: " + 1);
        }
        console.log("end: " + 2);
    }
    console.log("end: " + 3);
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!